【问题标题】:smoothly hide and show a button according to keyboard's availability根据键盘的可用性平滑隐藏和显示按钮
【发布时间】:2016-12-24 03:46:39
【问题描述】:

所以我在键盘出现时在键盘顶部显示一个按钮,然后在键盘关闭时将其隐藏,但动画流程不够流畅

我想用键盘上下(不阻塞用户界面)

图片:

当键盘出现时,您可以看到键盘仍未完全出现,但我的按钮在这里

当它隐藏时,仍然没有完全消失,但按钮消失了

我的代码:

代码:

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 
    }
}

【问题讨论】:

    标签: ios swift2 ios-animations


    【解决方案1】:

    你应该应用正确的动画:

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

    并且不要使用 0.3,而是从 UIKeyboardAnimationDurationUserInfoKey 中获取 timeInterval

    【讨论】:

    • 嘿伙计,它工作正常,但是 UIKeyboardAnimationDurationUserInfoKey 呢??
    • 你得到键盘出现动画的准确时间间隔(通常为 0.25)
    • 但是如何从UIKeyboardAnimationDurationUserInfoKey 获得它?它是一个字符串,对吗? public let UIKeyboardAnimationDurationUserInfoKey: String // NSNumber of double
    • 现在是 NSTimeInterval
    • 好的,知道了let duration: NSTimeInterval = (notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSTimeInterval)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 2011-08-24
    • 2018-03-29
    • 1970-01-01
    • 2016-08-19
    • 2012-03-28
    相关资源
    最近更新 更多