9

我正在尝试获取键盘的 UIAnimationCurve(如果存在)。我正在使用的代码如下:

if let kbCurve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? Int{
                animateCurve = UIViewAnimationCurve.
            }

但是,作为 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()
            }
        )

有没有办法使用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()
            }
        )

但是由于 UIViewAnimationOptions 不是正确的类型而发生编译错误。如果我自己运行 UIViewAnimationOptions 的分配,我会收到编译器错误“找不到接受提供的参数的 'init' 的重载”。

4

3 回答 3

20

Int您可以从原始值中获取枚举值

animateCurve = UIViewAnimationCurve(rawValue: kbCurve)

但是,标准键盘使用 7 的动画曲线值,这可能与枚举值不匹配,animateCurve因此nil. 如果是这种情况,只需定义animateCurve为 anInt并在代码中使用原始值而不是枚举值。

此外,快速谷歌搜索出现了这个包装器,这可能对你有用:https ://gist.github.com/kristopherjohnson/13d5f18b0d56b0ea9242

更新以回答已编辑的问题:

您可以通过将其转换为值来在动画选项中使用整数动画曲线UIViewAnimationOptions值:

UIViewAnimationOptions(kbCurve << 16)  // where kbCurve: UInt

Swift 1.2 (XCode 6.3) 的更新:

Swift 1.2 的发行说明表明,具有未记录值的 NS_ENUM 类型现在可以从其原始整数值转换,而无需重置为 nil。所以下面的代码现在可以工作了:

let animateCurve = UIViewAnimationCurve(rawValue: userInfo[UIKeyboardAnimationCurveUserInfoKey].integerValue)!

Swift 2.2 (XCode 7.3) 的更新:

if let animationCurveInt = (userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber)?.unsignedIntegerValue {
  let animationCurve = UIViewAnimationOptions(rawValue: animationCurveInt<<16)
  ...
}
于 2014-11-14T21:42:49.193 回答
3

斯威夫特 4 版本:

if let curveValue = (userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber)?.uintValue {
    let curveAnimationOptions = UIViewAnimationOptions(rawValue: curveValue << 16)
    //...
}
于 2018-11-07T20:12:32.723 回答
0

您应该使用较旧的 API,非基于块的 API。然后您将能够设置动画曲线,您将从通知对象中获取。

于 2014-11-15T00:28:56.303 回答