解散键盘时设置约束动画

时间:2018-09-02 13:43:13

标签: ios swift uicollectionview

我有一个UICollectionView,底部有一个UIView,其主要目的是充当UIButtonUITextView这对夫妇的容器。

现在,我已经设置了UIKeyboardWillShowUIKeyboardWillHide,它们通过为messageViewBottomConstraint设置动画来滑动容器而起作用。

    private func setKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

@objc func keyboardWillShow(_ notification: NSNotification) {
    let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
    guard let keyboardDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double else { return }
    UIView.animate(withDuration: keyboardDuration) {
        self.messageViewBottomConstraint.constant = -keyboardFrame!.height
        self.view.layoutIfNeeded()
    }
}

@objc func keyboardWillHide(_ notification: NSNotification) {
    guard let keyboardDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double else { return }
    UIView.animate(withDuration: keyboardDuration) {
        self.messageViewBottomConstraint.constant = 0
        self.view.layoutIfNeeded()
    }
}

我想实现UICollectionView的向下滑动功能,该功能允许您在向下滑动时消除关键字。

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.keyboardDismissMode = .interactive
}

问题是我不确定如何监视键盘框架在哪里,因此我可以为其制作动画messageViewBottomConstaint。否则,只有完全关闭键盘后,容纳UITextView的容器才能滑下来(这不是一个很好的UX)

0 个答案:

没有答案