How to force a raw value of 7 into a UIViewAnimationCurve enum?

淺唱寂寞╮ 提交于 2019-12-06 20:41:38

问题


The key UIKeyboardAnimationCurveUserInfoKey in the userInfo dictionary of a UIKeyboardWillShowNotification contains an Int with the value 7.

Now I need to pass this Int into UIView.setAnimationCurve(<here>). I tried to create the required UIViewAnimationCurve enum like this UIViewAnimationCurve(rawValue: 7). Because the raw value 7 is undocumented, the result is always nil.

It works fine this way in Objective-C. Any idea how to get this animation curve from the notification into a UIView animation using Swift?


Update: As pointed out by Martin, this is no longer a problem since Xcode 6.3.

From the Xcode 6.3 Release Notes:

Imported NS_ENUM types with undocumented values, such as UIViewAnimationCurve, can now be converted from their raw integer values using the init(rawValue:) initializer without being reset to nil. Code that used unsafeBitCast as a workaround for this issue can be written to use the raw value initializer.


回答1:


I think I figured it out, but I'm not sure if this is the way it's supposed to be done.

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


Update: The solution contained in this question works as well.

var animationCurve = UIViewAnimationCurve.EaseInOut
NSNumber(integer: 7).getValue(&animationCurve)


来源:https://stackoverflow.com/questions/25168413/how-to-force-a-raw-value-of-7-into-a-uiviewanimationcurve-enum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!