【问题标题】:UIView animation options using Swift使用 Swift 的 UIView 动画选项
【发布时间】:2014-07-27 16:28:07
【问题描述】:

如何在UIView 动画块中将UIViewAnimationOptions 设置为.Repeat

UIView.animateWithDuration(0.2, delay:0.2 , options: UIViewAnimationOptions, animations: (() -> Void), completion: (Bool) -> Void)?)

【问题讨论】:

    标签: uiviewanimation swift


    【解决方案1】:

    斯威夫特 3

    和之前差不多:

    UIView.animate(withDuration: 0.2, delay: 0.2, options: UIViewAnimationOptions.repeat, animations: {}, completion: nil)
    

    除了你可以省略完整类型:

    UIView.animate(withDuration: 0.2, delay: 0.2, options: .repeat, animations: {}, completion: nil)
    

    您仍然可以组合选项:

    UIView.animate(withDuration: 0.2, delay: 0.2, options: [.repeat, .curveEaseInOut], animations: {}, completion: nil)
    

    斯威夫特 2

    UIView.animateWithDuration(0.2, delay: 0.2, options: UIViewAnimationOptions.Repeat, animations: {}, completion: nil)
    
    UIView.animateWithDuration(0.2, delay: 0.2, options: .Repeat, animations: {}, completion: nil)
    
    UIView.animateWithDuration(0.2, delay: 0.2, options: [.Repeat, .CurveEaseInOut], animations: {}, completion: nil)
    

    【讨论】:

    • 在 Swift 3 中再次发生了变化。在 Swift 3 中可能看起来像这样:options: [.allowUserInteraction, .beginFromCurrentState],
    • swift 4怎么样?
    【解决方案2】:

    在 Swift 2.0 之前为枚举的大多数 Cocoa Touch '选项' 集现在已更改为结构,UIViewAnimationOptions 就是其中之一。

    UIViewAnimationOptions.Repeat 以前被定义为:

    (半伪代码)

    enum UIViewAnimationOptions {
      case Repeat
    }
    

    现在定义为:

    struct UIViewAnimationOption {
      static var Repeat: UIViewAnimationOption
    }
    

    要点是,为了实现之前使用位掩码 (.Reverse | .CurveEaseInOut) 所实现的效果,您现在需要将选项放在一个数组中,或者直接在 options 参数之后,或者在之前的变量中定义使用它:

    UIView.animateWithDuration(0.2, delay: 0.2, options: [.Repeat, .CurveEaseInOut], animations: {}, completion: nil)
    

    let options: UIViewAnimationOptions = [.Repeat, .CurveEaseInOut]
    UIView.animateWithDuration(0.2, delay: 0.2, options: options, animations: {}, completion: nil)
    

    更多信息请参考用户@0x7fffffff 的以下回答:Swift 2.0 - Binary Operator “|” cannot be applied to two UIUserNotificationType operands

    【讨论】:

    • 你知道你可以稍微编辑一下答案...... :)
    【解决方案3】:

    斯威夫特 5

    UIView.animate(withDuration: 0.2, delay: 0, options: UIView.AnimationOptions.curveEaseInOut, animations: {}, completion: nil)
    

    【讨论】:

      猜你喜欢
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      相关资源
      最近更新 更多