iOS:如何将 UIViewAnimationCurve 转换为 UIViewAnimationOptions?
UIKeyboardAnimationCurveUserInfoKey
有一个 UIViewAnimationCurve
值。如何将其转换为相应的 UIViewAnimationOptions
值,以便与 +[UIView animateWithDuration:delay:options:animations:completion:]
options 参数一起使用代码>?
// 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;
显然,我可以使用 switch
语句创建一个简单的类别方法,如下所示:
// 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
但是,有没有更简单/更好的方法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您建议的类别方法是“正确”的方法 - 您不一定能保证这些常量保持其值。不过,从查看它们的定义方式来看,如果编译器想抱怨这一点,您似乎可以
……可能先转换为 NSUInteger,然后转换为 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
...possibly with a cast to NSUInteger and then to UIViewAnimationOptions, if the compiler feels like complaining about that.
可以说,您可以采用第一个解决方案并将其设为内联函数,以节省堆栈推送的时间。这是一个非常严格的条件(常量绑定等),它应该编译成一个非常小的程序集。
编辑:
根据@matt,给你(Objective-C):
Swift 3:
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):
Swift 3:
在 Swift 中你可以做
In Swift you can do
基于开关的解决方案的一个问题是,它假设不会传入任何选项组合。但实践表明,在某些情况下该假设可能不成立。我发现的一个例子是(至少在 iOS 7 上)当您获取键盘动画来为您的内容以及键盘的出现/消失设置动画时。
如果您收听
keyboardWillShow:
或keyboardWillHide:
通知,然后获取键盘宣布将使用的曲线,例如:您可能会获得值 7。如果将其传递到 switch 函数/方法中,您将无法获得该值的正确转换,从而导致不正确的动画行为。
Noah Witherspoon 的答案将返回正确的值。结合这两种解决方案,您可能会写出如下内容:
这里需要注意的是,正如 Noah 也指出的那样,如果 Apple 更改了两种类型不再对应的枚举,那么该函数将会中断。无论如何使用它的原因是,基于开关的选项并不适用于您今天可能遇到的所有情况,而这却可以。
An issue with the switch based solution is that it assumes no combination of options will be ever passed in. Practice shows though, that there may be situations where the assumption doesn't hold. One instance I found is (at least on iOS 7) when you obtain the keyboard animations to animate your content along with the appearance/disappearance of the keyboard.
If you listen to the
keyboardWillShow:
orkeyboardWillHide:
notifications, and then get the curve the keyboard announces it will use, e.g:you're likely to obtain the value 7. If you pass that into the switch function/method, you won't get a correct translation of that value, resulting in incorrect animation behaviour.
Noah Witherspoon's answer will return the correct value. Combining the two solutions, you might write something like:
The caveat here, as noted by Noah also, is that if Apple ever changes the enumerations where the two types no longer correspond, then this function will break. The reason to use it anyway, is that the switch based option doesn't work in all situations you may encounter today, while this does.
iOS 10+
Swift 5
将
UIView.AnimationCurve
转换为UIView.AnimationOptions
的 Swift 替代方案(甚至可能不可能)是使用UIViewPropertyAnimator
(iOS 10+),它接受UIView.AnimationCurve
并且是比UIView.animate
更现代的动画制作器。您很可能会使用
UIResponder.keyboardAnimationCurveUserInfoKey
,它返回一个NSNumber
。该密钥的文档是(苹果自己的符号,不是我的):使用这种方法,我们可以消除任何猜测:
iOS 10+
Swift 5
A Swift alternative to converting
UIView.AnimationCurve
toUIView.AnimationOptions
, which may not even be possible, is to useUIViewPropertyAnimator
(iOS 10+), which acceptsUIView.AnimationCurve
and is a more modern animator thanUIView.animate
.Most likely you'll be working with
UIResponder.keyboardAnimationCurveUserInfoKey
, which returns anNSNumber
. The documentation for this key is (Apple's own notation, not mine):Using this approach, we can eliminate any guesswork: