iOS: How to convert UIViewAnimationCurve to UIViewAnimationOptions? iOS: How to convert UIViewAnimationCurve to UIViewAnimationOptions? ios ios

iOS: How to convert UIViewAnimationCurve to UIViewAnimationOptions?


The category method you suggest is the “right” way to do it—you don’t necessarily have a guarantee of those constants keeping their value. From looking at how they’re defined, though, it seems you could just do

animationOption = animationCurve << 16;

...possibly with a cast to NSUInteger and then to UIViewAnimationOptions, if the compiler feels like complaining about that.


Arguably you can take your first solution and make it an inline function to save yourself the stack push. It's such a tight conditional (constant-bound, etc) that it should compile into a pretty tiny piece of assembly.

Edit:Per @matt, here you go (Objective-C):

static inline UIViewAnimationOptions animationOptionsWithCurve(UIViewAnimationCurve curve){  switch (curve) {    case UIViewAnimationCurveEaseInOut:        return UIViewAnimationOptionCurveEaseInOut;    case UIViewAnimationCurveEaseIn:        return UIViewAnimationOptionCurveEaseIn;    case UIViewAnimationCurveEaseOut:        return UIViewAnimationOptionCurveEaseOut;    case UIViewAnimationCurveLinear:        return UIViewAnimationOptionCurveLinear;  }}

Swift 3:

extension UIViewAnimationOptions {    init(curve: UIViewAnimationCurve) {        switch curve {            case .easeIn:                self = .curveEaseIn            case .easeOut:                self = .curveEaseOut            case .easeInOut:                self = .curveEaseInOut            case .linear:                self = .curveLinear        }    }}


In Swift you can do

extension UIViewAnimationCurve {    func toOptions() -> UIViewAnimationOptions {        return UIViewAnimationOptions(rawValue: UInt(rawValue << 16))    }}