UIView animation options using Swift UIView animation options using Swift swift swift

UIView animation options using Swift


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)


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


Swift 5

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