7

这是我到目前为止尝试过的

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}}.

4

1 回答 1

10

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
于 2014-08-02T04:54:46.223 回答