簡體   English   中英

通過滾動以交互方式解除鍵盤高度

[英]Get keyboard height while being dismissed interactively with scroll

我正在創建一個聊天界面,就像WhatsApp一樣,我創建了一個“scrollToBottom”按鈕,當用戶以一定距離滾動集合時會出現該按鈕。 當鍵盤出現時,此按鈕完全跟隨鍵盤框架,當消失時,唯一的問題是當鍵盤被交互式解除時,我無法使該按鈕跟隨鍵盤框架。 只有在隱藏鍵盤后,系統才會發送通知並且按鈕會更改其常量。

我已經嘗試了所有鍵盤通知,但沒有一個能幫我解決這個問題。 我需要一些及時的東西,使按鈕跟隨鍵盤沒有任何延遲。 甚至UIKeyboardWillChangeFrameNotification都沒有對我UIKeyboardWillChangeFrameNotification

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

private func configureConstantViewNewMessages(notification: NSNotification){
        if let userInfo = notification.userInfo {
            let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
            let keyboardEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
            let convertedKeyboardEndFrame = view.convertRect(keyboardEndFrame, fromView: view.window)
            let rawAnimationCurve = (notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedIntValue << 16
            let animationCurve = UIViewAnimationOptions(rawValue: UInt(rawAnimationCurve))

            self.kNewMessages.constant = CGRectGetMaxY(view.bounds) - CGRectGetMinY(convertedKeyboardEndFrame) + 10

            UIView.animateWithDuration(animationDuration, delay: 0.0, options: [.BeginFromCurrentState, animationCurve], animations: {
                self.view.layoutIfNeeded()
                }, completion: nil)
        }
    }

使用上面的代碼,我調用configureConstantViewNewMessages方法來設置我的按鈕常量(kNewMessages)的動畫,它可以根據鍵盤高度更改其位置。

感謝您對英語錯誤的支持和抱歉。

請使用下面的代碼可能適合您。

 override func viewWillAppear(animated: Bool) {
           NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
 }



func keyboardWillShow(notification:NSNotification) {
        let userInfo:NSDictionary = notification.userInfo!
        let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.CGRectValue()
        let keyboardHeight = keyboardRectangle.height
        print(keyboardHeight)
    }

當鍵盤消失時,添加觀察者以獲取鍵盤位置的通知。

此通知將為您提供包含有關鍵盤寬度和高度的信息的用戶信息

您應該使用此文檔鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM