UIViewAnimationOptions

UIViewAnimationOptions

本文介绍了iOS:如何将UIViewAnimationCurve转换为UIViewAnimationOptions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIKeyboardAnimationCurveUserInfoKey 有一个 UIViewAnimationCurve 值。如何将其转换为与选项参数 +相关的 UIViewAnimationOptions 值UIView.h

typedef枚举{
UIViewAnimationCurveEaseInOut,//开始和结束时缓慢
UIViewAnimationCurveEaseIn,//开始时缓慢
UIViewAnimationCurveEaseOut,//在结束时缓慢
UIViewAnimationCurveLinear
} UIViewAnimationCurve;

// ...

枚举{
// ...
UIViewAnimationOptionCurveEaseInOut = 0< 16,// default
UIViewAnimationOptionCurveEaseIn = 1<< 16,
UIViewAnimationOptionCurveEaseOut = 2<< 16,
UIViewAnimationOptionCurveLinear = 3< 16,
// ...
};
typedef NSUInteger UIViewAnimationOptions;

显然,我可以创建一个简单的类别方法,一个开关语句,如下所示:

  // UIView + AnimationOptionsWithCurve.h 

@interface UIView(AnimationOptionsWithCurve)
@end

// UIView + AnimationOptionsWithCurve.m

@implementation UIView(AnimationOptionsWithCurve)

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

@end

但是有没有一个更简单/更好的方法?

解决方案

可以说你可以采取你的第一个解决方案,使它成为一个内联函数来保存自己堆栈推。这是一个紧凑的条件(常量等),它应该编译成一个很小的程序集。



编辑:
每个@matt,在这里你去(Objective-C):

 静态内联UIViewAnimationOptions animationOptionsWithCurve(UIViewAnimationCurve曲线)
{
开关(曲线){
case UIViewAnimationCurveEaseInOut:
return UIViewAnimationOptionCurveEaseInOut;
case UIViewAnimationCurveEaseIn:
return UIViewAnimationOptionCurveEaseIn;
case UIViewAnimationCurveEaseOut:
return UIViewAnimationOptionCurveEaseOut;
case UIViewAnimationCurveLinear:
return UIViewAnimationOptionCurveLinear;
}
}

Swift 3:

 扩展UIViewAnimationOptions {
init(曲线:UIViewAnimationCurve){
开关曲线{
case .easeIn:
self = .curveEaseIn
case .easeOut:
self = .curveEaseOut
case .easeInOut:
self = .curveEaseInOut
case .linear:
self =。 curveLinear
}
}
}


The UIKeyboardAnimationCurveUserInfoKey has a UIViewAnimationCurve value. How do I convert it to the corresponding UIViewAnimationOptions value for use with the options argument of +[UIView animateWithDuration:delay:options:animations:completion:]?

// UIView.h

typedef enum {
    UIViewAnimationCurveEaseInOut,         // slow at beginning and end
    UIViewAnimationCurveEaseIn,            // slow at beginning
    UIViewAnimationCurveEaseOut,           // slow at end
    UIViewAnimationCurveLinear
} UIViewAnimationCurve;

// ...

enum {
    // ...
    UIViewAnimationOptionCurveEaseInOut            = 0 << 16, // default
    UIViewAnimationOptionCurveEaseIn               = 1 << 16,
    UIViewAnimationOptionCurveEaseOut              = 2 << 16,
    UIViewAnimationOptionCurveLinear               = 3 << 16,
    // ...
};
typedef NSUInteger UIViewAnimationOptions;

Obviously, I could create a simple category method with a switch statement, like so:

// UIView+AnimationOptionsWithCurve.h

@interface UIView (AnimationOptionsWithCurve)
@end

// UIView+AnimationOptionsWithCurve.m

@implementation UIView (AnimationOptionsWithCurve)

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

@end

But, is there an even easier/better way?

解决方案

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
        }
    }
}

这篇关于iOS:如何将UIViewAnimationCurve转换为UIViewAnimationOptions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 07:11