UIView animation options using Swift

后端 未结 3 1439
傲寒
傲寒 2021-01-31 02:17

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

UIView.animateWithDuration(0.2, delay:0.2         


        
相关标签:
3条回答
  • 2021-01-31 03:03

    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)
    
    0 讨论(0)
  • 2021-01-31 03:08

    Swift 5

    UIView.animate(withDuration: 0.2, delay: 0, options: UIView.AnimationOptions.curveEaseInOut, animations: {}, completion: nil)
    
    0 讨论(0)
  • 2021-01-31 03:13

    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

    0 讨论(0)
提交回复
热议问题