键盘隐藏时的Swift ios动画看起来很奇怪

时间:2016-04-17 13:57:56

标签: ios swift keyboard nsnotificationcenter

当键盘使用以下代码显示/隐藏时,我正在移动我的文本字段:

//View did load
//Move keyboard
        NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: #selector(LoginViewController.keyboardWillShow(_:)),
            name: UIKeyboardWillShowNotification,
            object: nil
        )
        NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: #selector(LoginViewController.keyboardWillHide(_:)),
            name: UIKeyboardWillHideNotification,
            object: nil
        )

deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
    }


func keyboardWillShow(notification: NSNotification) {

        // Pull a bunch of info out of the notification
        if let scrollView = ScrollView, userInfo = notification.userInfo, endValue = userInfo[UIKeyboardFrameEndUserInfoKey], durationValue = userInfo[UIKeyboardAnimationDurationUserInfoKey], curveValue = userInfo[UIKeyboardAnimationCurveUserInfoKey] {

            // Transform the keyboard's frame into our view's coordinate system
            let endRect = view.convertRect(endValue.CGRectValue, fromView: view.window)

            // Find out how much the keyboard overlaps the scroll view
            let keyboardOverlap = scrollView.frame.maxY - endRect.origin.y + 20

            // Set the scroll view's content inset to avoid the keyboard
            // Don't forget the scroll indicator too!
            scrollView.contentInset.bottom = keyboardOverlap
            scrollView.scrollIndicatorInsets.bottom = keyboardOverlap

            let duration = durationValue.doubleValue
            let options = UIViewAnimationOptions(rawValue: UInt(curveValue.integerValue << 16))
            UIView.animateWithDuration(duration, delay: 0, options: options, animations: {
                self.view.layoutIfNeeded()
                }, completion: nil)
        }
    }

    func keyboardWillHide(notification: NSNotification) {

        // Pull a bunch of info out of the notification
        if let scrollView = ScrollView, userInfo = notification.userInfo, endValue = userInfo[UIKeyboardFrameEndUserInfoKey], durationValue = userInfo[UIKeyboardAnimationDurationUserInfoKey], curveValue = userInfo[UIKeyboardAnimationCurveUserInfoKey] {

            // Transform the keyboard's frame into our view's coordinate system
            let endRect = view.convertRect(endValue.CGRectValue, fromView: view.window)

            // Find out how much the keyboard overlaps the scroll view
            let keyboardOverlap = scrollView.frame.maxY - endRect.origin.y - 20

            // Set the scroll view's content inset to avoid the keyboard
            // Don't forget the scroll indicator too!
            scrollView.contentInset.bottom = keyboardOverlap
            scrollView.scrollIndicatorInsets.bottom = keyboardOverlap

            let duration = durationValue.doubleValue
            let options = UIViewAnimationOptions(rawValue: UInt(curveValue.integerValue << 16))
            UIView.animateWithDuration(duration, delay: 0, options: options, animations: {
                self.view.layoutIfNeeded()
                }, completion: nil)
        }
    }

当键盘显示或隐藏时,这将移动我的视图。当键盘显示视图的动画是平滑的,但是当我完成写入并且键盘隐藏视图的动画似乎不起作用时,视图只是简单地跳回原来的位置,这是正确的,但我会喜欢动画它所以它移动缓慢

0 个答案:

没有答案