2016-08-01 171 views
1

我已經註冊了兩個UIKeyboardNotifications(keyboardWillShow和keyboardWillHide),兩者都被觸發。問題出現在文本字段移回原始位置的動畫僅在鍵盤消失後被觸發。無論如何減少獲取通知和動畫文本字段之間的延遲?UIKeyboardWillHideNotification動畫延遲

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)