UIKeyboardWillHideNotification延迟动画

时间:2016-08-01 11:04:38

标签: ios swift nsnotificationcenter uikeyboard

我已经注册了两个UIKeyboardNotifications(keyboardWillShow和keyboardWillHide),两者都被触发了。当键盘消失后,只有在文本字段移动回原始位置的动画才会被触发时,会出现此问题。无论如何要减少获取通知和动画文本字段之间的延迟吗?

func keyboardWillShow(notification: NSNotification) {
    let info = notification.userInfo!
    let keyboardFrame : CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    duration = (info[UIKeyboardAnimationDurationUserInfoKey]?.doubleValue)
    let rawAnimationCurve = (notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedIntValue << 16
    animationCurve = UIViewAnimationOptions(rawValue: UInt(rawAnimationCurve))
    let moveAmount = keyboardFrame.height
    UIView.animateWithDuration(duration!, delay:0, options: animationCurve, animations: {
        self.txtfield.transform = CGAffineTransformMakeTranslation(0.0, -self.moveAmount)
        }, completion:nil)
}

func keyboardWillHide(notification: NSNotification) {
        UIView.animateWithDuration(duration, delay:0, options:animationCurve, animations: {
            self. txtfield.transform = CGAffineTransformMakeTranslation(0.0, 0.0)
            }, completion:nil)
}

GIF image here

1 个答案:

答案 0 :(得分:1)

实现了一个问题,这是我的一个粗心的错误。

补充:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(viewController.keyboardDidHide), name: UIKeyboardWillHideNotification, object: nil)

而不是:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(viewController.keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)