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双# –