美文网首页
[RxSwift] 键盘通知

[RxSwift] 键盘通知

作者: 巨馍蘸酱 | 来源:发表于2023-03-24 12:59 被阅读0次

        //监听键盘通知
        NotificationCenter.default.rx
            .notification(UIResponder.keyboardWillChangeFrameNotification)
            .take(until: self.rx.deallocated) //页面销毁自动移除通知监听
            .subscribe(onNext: { [weak self] notification in
//                AnyHashable("UIKeyboardFrameEndUserInfoKey"): NSRect: {{0, 476}, {375, 336}},
//                AnyHashable("UIKeyboardFrameEndUserInfoKey"): NSRect: {{0, 812}, {375, 336}},
//                AnyHashable("UIKeyboardFrameEndUserInfoKey"): NSRect: {{0, 812}, {375, 233}},
                guard let `self` = self,
                      let userInfo = notification.userInfo,
                      let value = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue,
                      let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
                      let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt
                else { return }
                
                let frame = value.cgRectValue // 键盘 fram
                let intersection = frame.intersection(self.view.frame) // 交集
                let keyboardHeight = intersection.height
                
                UIView.animate(withDuration: duration, delay: 0.0,
                               options: UIView.AnimationOptions(rawValue: curve), animations: {
                    if keyboardHeight > 0 {
                        self.inputBottom.update(inset: keyboardHeight)
                    } else {
                        self.inputBottom.update(inset: self.view.safeAreaInsets.bottom)
                    }
                }, completion: nil)
                
            }).disposed(by: disposeBag)

相关文章

网友评论

      本文标题:[RxSwift] 键盘通知

      本文链接:https://www.haomeiwen.com/subject/lhnwrdtx.html