这是我到目前为止尝试过的方法
func onKeyboardRaise(notification: NSNotification) {
var notificationData = notification.userInfo
var duration = notificationData[UIKeyboardAnimationDurationUserInfoKey] as NSNumber
var frame = notificationData[UIKeyboardFrameBeginUserInfoKey]! as NSValue
var frameValue :UnsafePointer<(CGRect)> = nil;
frame.getValue(frameValue)
}
但我似乎总是在 frame.getValue(frameValue)
处崩溃。
这有点令人困惑,因为 UIKeyboardFrameBeginUserInfoKey
的文档说它返回一个 CGRect
对象,但是当我在控制台中记录 frame
时,它声明类似 NSRect {{x, y}, {w, h}}
。
最佳答案
getValue()
必须使用指向(初始化的)变量的指针来调用
大小合适:
var frameValue = CGRect(x: 0, y: 0, width: 0, height: 0)
frame.getValue(&frameValue)
但是使用便捷方法更简单:
let frameValue = frame.CGRectValue() // Swift 1, 2
let frameValue = frame.cgRectValue() // Swift 3
关于ios - 如何在 Swift 中从 NSValue 获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25091765/