UIKeyboardWillHideNotification 动画延迟

Posted

技术标签:

【中文标题】UIKeyboardWillHideNotification 动画延迟【英文标题】:UIKeyboardWillHideNotification delay in animation 【发布时间】:2016-08-01 11:04:37 【问题描述】:

我已经注册了两个 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】:

意识到一个问题,这是我一个粗心的错误。

添加:

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

而不是:

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

【讨论】:

以上是关于UIKeyboardWillHideNotification 动画延迟的主要内容,如果未能解决你的问题,请参考以下文章