2016-08-18 78 views
1

好吧所以這個問題類似於this其中一個,但我跟着這個問題的答案,但我沒有工作,所以事情是,我有一個Textfield在我的查看,我想,當鍵盤出現這種上移是我的代碼:動畫第一次運行不順暢(鍵盤出現時)

通知觀察員鍵盤的狀態

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LaunchScreenViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil) 

功能讓鍵盤的高度

func keyboardWillShow(notification:NSNotification) { 
    let userInfo:NSDictionary = notification.userInfo! 
    duration = (notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double) 
    let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue 
    let keyboardRectangle = keyboardFrame.CGRectValue() 

    keyboardHeight = keyboardRectangle.height 

} 

我的動畫

func textFieldDidBeginEditing(textField: UITextField) { 

    self.nextButtonConstraint.constant = keyboardHeight 
    UIView.animateWithDuration(duration) { 
     self.nextButton.layoutIfNeeded() 
     self.emailTextField.layoutIfNeeded() 

    } 

} 

,你可以看到我的動畫是textFieldDidBeginEditing因爲根據類似question'的答案把它會有解決的問題,但在第一次運行仍然(當鍵盤第一次出現)我的動畫不流暢

+0

此代碼是否在UIViewController子類中?嘗試調用'self.view.layoutIfNeeded'而不是在'nextButton'和'emailTextField'上調用它。 – Connor

+0

@ConnorNeville,感謝您回覆的人是啊,他們是'UIViewController'類的內部,我厭倦了你的建議仍然沒有改變 –

+0

嗯。嘗試爲'keyboardDidShow'添加另一個'NSNotificationCenter'觀察者,並將動畫放在那裏。 – Connor

回答

0

我覺得解決方案可能很簡單。 嘗試把你的代碼的這裏面:

DispatchQueue.main.async{ 
    // Your Code here 
    } 

這使動畫爲主線,應加快步伐,避免不平滑的動畫。