ma1*_*w28 5 enums types casting nsnumber swift
我如何获得下面的行编译?
UIView.setAnimationCurve(userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue)
Run Code Online (Sandbox Code Playgroud)
现在,它给出了编译错误:
'NSNumber' is not a subtype of 'UIViewAnimationCurve'
Run Code Online (Sandbox Code Playgroud)
为什么编译器认为userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue是NSNumber什么时候integerValue被声明为Int?
class NSNumber : NSValue {
// ...
var integerValue: Int { get }`?
// ...
}
Run Code Online (Sandbox Code Playgroud)
我与其他枚举类型有类似的问题.什么是一般解决方案?
ma1*_*w28 14
UIView.setAnimationCurve(UIViewAnimationCurve.fromRaw(userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue)!)
Run Code Online (Sandbox Code Playgroud)
阅读更多:Swift枚举和fromRaw()
基于如何使用默认iOS7 UIAnimation曲线的答案,我使用新的基于块的动画方法+ animateWithDuration:delay:options:animations:completion:,并得到如下UIViewAnimationOptions所示:
let options = UIViewAnimationOptions(UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as NSNumber).integerValue << 16))
Run Code Online (Sandbox Code Playgroud)