2016-08-17 70 views
1

所以我展示上,當鍵盤出現,然後隱藏它,當鍵盤被解僱,但動畫的流量是不夠光滑順利隱藏和顯示一個按鈕,按鍵盤上的可用性

我的鍵盤頂部的按鈕i要它來上下鍵盤(不阻塞UI)

圖片:

WHEN鍵盤出現時,你可以看到,鍵盤仍然不FULLLY出現但我的按鈕,這裏

enter image description here

當它隱藏,還沒有完全撤職,但按鈕消失

enter image description here

我的代碼:

CODE:

viewDidLoad ...... { 
     // here we have notification observers for tracking the states of Keyboard 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LaunchScreenViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil) 

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

} 


    func keyboardWillShow(notification:NSNotification) { // in this function i'm changing the origin (Y) axis of my button os that it can appear on top of my keyboard 
    let userInfo:NSDictionary = notification.userInfo! 
    let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue 
    let keyboardRectangle = keyboardFrame.CGRectValue() 
    UIView.animateWithDuration(0.3) { 

     self.nextButtonConstraint.constant = keyboardRectangle.height 
    } 
} 

func keyboardWillHide(notification:NSNotification) { 
    UIView.animateWithDuration(0.5) { 

     self.nextButtonConstraint.constant = -50 // here i'm making my button out of screen bounds 
    } 
} 

回答

1

你應該申請正確的動畫:

self.nextButtonConstraint.constant = keyboardRectangle.height 
UIView.animateWithDuration(0.3) { 
    <outlet to nextButton>.layoutIfNeeded() // insert correct value in <> 
} 

,不要使用0.3,但是從UIKeyboardAnimationDurationUserInfoKey

+0

需要一個時間間隔嗨,哥們其工作正常,但有什麼用'UIKeyboardAnimationDurationUserInfoKey'? –

+0

你得到確切的timeInterval鍵盤外觀(通常爲0.25) – Igor

+0

動畫,但如何從'UIKeyboardAnimationDurationUserInfoKey'獲取它?它的字符串是正確的? '讓公共讓UIKeyboardAnimationDurationUserInfoKey:字符串// NSNumber雙# –