13

UIKeyboardAnimationCurveUserInfoKeya 的 userInfo 字典中的键UIKeyboardWillShowNotification包含Int值为 的7

现在我需要将它传递IntUIView.setAnimationCurve(<here>). 我试图像这样创建所需的 UIViewAnimationCurve 枚举UIViewAnimationCurve(rawValue: 7)。由于原始值7未记录,因此结果始终为nil.

它在 Objective-C 中以这种方式工作得很好。知道如何使用 Swift 将通知中的动画曲线转换为 UIView 动画吗?


更新: 正如 Martin 所指出的,这不再是 Xcode 6.3 以来的问题。

来自Xcode 6.3 发行说明

具有未记录值的导入NS_ENUM类型,例如UIViewAnimationCurve,现在可以使用初始值设定项从其原始整数值转换,init(rawValue:)而无需重置为nil. unsafeBitCast可以编写用作解决此问题的代码以使用原始值初始化程序。

4

1 回答 1

9

我想我想通了,但我不确定这是否应该这样做。

let animationCurve = unsafeBitCast(7, UIViewAnimationCurve.self)
UIView.setAnimationCurve(animationCurve)


更新:这个问题中包含的解决方案也有效。

var animationCurve = UIViewAnimationCurve.EaseInOut
NSNumber(integer: 7).getValue(&animationCurve)
于 2014-08-06T21:00:56.497 回答