UIView animation options using Swift

♀尐吖头ヾ 提交于 2019-12-03 04:32:41

问题


How do I set the UIViewAnimationOptions to .Repeat in an UIView animation block:

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

回答1:


Swift 3

Pretty much the same as before:

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

except that you can leave out the full type:

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

and you can still combine options:

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)



回答2:


Most of the Cocoa Touch 'option' sets that were enums prior to Swift 2.0 have now been changed to structs, UIViewAnimationOptions being one of them.

Whereas UIViewAnimationOptions.Repeat would previously have been defined as:

(semi-pseudo code)

enum UIViewAnimationOptions {
  case Repeat
}

It is now defined as:

struct UIViewAnimationOption {
  static var Repeat: UIViewAnimationOption
}

Point being, in order to achieve what was achieved prior using bitmasks (.Reverse | .CurveEaseInOut) you'll now need to place the options in an array, either directly after the options parameter, or defined in a variable prior to utilising it:

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

or

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

Please refer to the following answer from user @0x7fffffff for more information: Swift 2.0 - Binary Operator “|” cannot be applied to two UIUserNotificationType operands



来源:https://stackoverflow.com/questions/24081192/uiview-animation-options-using-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!