使用Swift的UIView动画选项

时间:2014-06-06 11:51:03

标签: uiviewanimation swift

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

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

3 个答案:

答案 0 :(得分:102)

Swift 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)

Swift 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)

答案 1 :(得分:13)

大多数Cocoa Touch'选项'在Swift 2.0之前作为枚举的集合现在已经更改为结构,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

答案 2 :(得分:0)

快捷键5

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