这是我到目前为止所尝试的内容
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对象,但是当我在控制台中记录帧时,它会声明类似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