ste*_*ris 7 keyboard uiviewanimation ios uiviewanimation-curve swift
我试图获得键盘的UIAnimationCurve,如果它存在的话.我使用的代码如下:
if let kbCurve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? Int{
animateCurve = UIViewAnimationCurve.
}
Run Code Online (Sandbox Code Playgroud)
但是,作为UIViewAnimationCurve的animateCurve无法从Int转换.我如何以这种方式获得曲线?
如果我将它们视为数字,而不是UIViewAnimationCurve枚举,则在尝试动画时出现以下错误:
//Animated done button with keyboard
origDoneFrame = btnDone.frame
btnDone.hidden = false
UIView.animateWithDuration(
animateDuration,
delay: Numbers.ANIMATE_DELAY,
options: nil,
animations: {
UIView.setAnimationCurve(animateCurve)
self.btnDone.frame = CGRectMake(self.btnDone.frame.origin.x + kbHeight, self.btnDone.frame.origin.y, self.btnDone.frame.size.width, self.btnDone.frame.size.height)
return Void()
},
completion: {finished in
return Void()
}
)
Run Code Online (Sandbox Code Playgroud)
有没有办法使用int设置曲线?
尝试使用int曲线:
UIView.animateWithDuration(
animateDuration,
delay: Numbers.ANIMATE_DELAY,
options: UIViewAnimationOptions(animateCurve << 16),
animations: {
self.btnDone.frame = CGRectMake(self.btnDone.frame.origin.x + kbHeight, self.btnDone.frame.origin.y, self.btnDone.frame.size.width, self.btnDone.frame.size.height)
return Void()
},
completion: {finished in
return Void()
}
)
Run Code Online (Sandbox Code Playgroud)
但是由于UIViewAnimationOptions不是正确类型而发生编译错误.如果我自己运行UIViewAnimationOptions的赋值,我得到编译器错误"找不到接受提供的参数的'init'的重载."
nic*_*aef 18
您可以使用原始Int值获取枚举值
animateCurve = UIViewAnimationCurve(rawValue: kbCurve)
Run Code Online (Sandbox Code Playgroud)
但是,标准键盘使用的动画曲线值为7,可能与枚举值不匹配,因此animateCurve也是如此nil.如果是这种情况,只需将其定义animateCurve为Int并使用代码中的原始值而不是枚举值.
此外,快速谷歌搜索打开了这个包装,这可能对您有用:https://gist.github.com/kristopherjohnson/13d5f18b0d56b0ea9242
更新以回答已编辑的问题:
您可以通过将动画选项转换为值来在动画选项中使用整数动画曲线UIViewAnimationOptions值:
UIViewAnimationOptions(kbCurve << 16) // where kbCurve: UInt
Run Code Online (Sandbox Code Playgroud)
Swift 1.2更新(XCode 6.3):
Swift 1.2的发行说明表明,具有未记录值的NS_ENUM类型现在可以从其原始整数值转换而不会重置为nil.所以下面的代码现在可以工作:
let animateCurve = UIViewAnimationCurve(rawValue: userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue)!
Run Code Online (Sandbox Code Playgroud)
Swift 2.2更新(XCode 7.3):
if let animationCurveInt = (userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber)?.unsignedIntegerValue {
let animationCurve = UIViewAnimationOptions(rawValue: animationCurveInt<<16)
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5099 次 |
| 最近记录: |