【问题标题】:Keyboard Animation Curve as Int键盘动画曲线为 Int
【发布时间】:2015-01-12 09:10:43
【问题描述】:

我正在尝试获取键盘的 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' 的重载。”

【问题讨论】:

  • 你试过用as? UIViewAnimationCurve代替吗?
  • @IanMacDonald 那不会编译。该错误与实际问题无关(我注意到 XCode 经常出现这种情况,但 7 与枚举不匹配(请参阅下面 nickgraef 的回复)

标签: ios swift keyboard uiviewanimation uiviewanimation-curve


【解决方案1】:

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

animateCurve = UIViewAnimationCurve(rawValue: kbCurve)

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

此外,快速的 Google 搜索出现了这个包装器,它可能对您有用: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)
  ...
}

【讨论】:

  • 你是对的。 7 与枚举不匹配。但是,当我尝试执行上述操作时(请参阅编辑后的帖子),它会在 UIView.setAnimationCurve 上出错。
  • @steventnorris 我已经更新了我的答案,以展示如何将 7 值用作动画曲线。
  • 我已经尝试过您的解决方案,但出现编译器错误。我已经调整了上面的帖子。
  • @steventnorris 你需要位移kbCurve(一个Int),而不是animateCurve(一个枚举值)
  • 你如何在 Swift 2 中做到这一点?
【解决方案2】:

Swift 4 版本:

if let curveValue = (userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber)?.uintValue {
    let curveAnimationOptions = UIViewAnimationOptions(rawValue: curveValue << 16)
    //...
}

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多