123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- //
- // KeyBoardCommentView.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2019/7/11.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- //
- import UIKit
- import IQKeyboardManagerSwift
- import RxSwift
- import FWPopupView
- class KeyBoardCommentView: FWPopupView {
-
- let disposeBag = DisposeBag()
- /// 上下间距
- let topOrBottomEdge : CGFloat = 10
- /// 输入框宽度
- let inputTextViewWidth = kScreenWidth - 14 - 84
- /// 键盘的高度
- var keyboardH : CGFloat = 0
- /// 动画时长
- var duration : Double = 0
-
- /// 显示的View
- var showView : UIView?
-
- override init(frame: CGRect) {
- super.init(frame: frame)
- setupViews()
- setupData()
-
- configRectCorner(corner: [.topLeft,.topRight], radii: CGSize(width: 8, height: 8))
- }
-
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- func setupViews() {
- backgroundColor = UIColor.white
- addSubview(inputTextView)
- addSubview(sendButton)
- }
-
- lazy var inputTextView: IQTextView = {
- let inputTextView = IQTextView(frame: CGRect(x: 14, y: topOrBottomEdge, width: inputTextViewWidth, height: 28))
- inputTextView.backgroundColor = kf0f0f0Color
- inputTextView.font = kRegularFont14
- inputTextView.layoutManager.allowsNonContiguousLayout = false
- inputTextView.enablesReturnKeyAutomatically = true
- inputTextView.scrollsToTop = false
- inputTextView.textContainerInset = UIEdgeInsets(top: 5, left: 15, bottom: 5, right: 10)
- inputTextView.textContainer.lineFragmentPadding = 0
- inputTextView.layer.cornerRadius = 14
- inputTextView.layer.masksToBounds = true
- inputTextView.delegate = self
- inputTextView.tintColor = kThemeColor
- return inputTextView
- }()
-
- lazy var sendButton: UIButton = {
- let sendButton = UIButton(type: UIButton.ButtonType.custom)
- sendButton.frame = CGRect(x: kScreenWidth - 14 - 60, y: 11, width: 60, height: 28)
- sendButton.setTitle("发送", for: UIControl.State.normal)
- sendButton.setBackgroundImage(UIImage.imageWithColor(color: kThemeColor), for: UIControl.State.normal)
- sendButton.setBackgroundImage(UIImage.imageWithColor(color: kd8d8d8Color), for: UIControl.State.disabled)
- sendButton.titleLabel?.font = kRegularFont14
- sendButton.layer.cornerRadius = 14;
- sendButton.layer.masksToBounds = true
- sendButton.isEnabled = true
- return sendButton
- }()
-
- func setupData() {
- //监听键盘弹出通知
- _ = NotificationCenter.default.rx
- .notification(UIResponder.keyboardWillShowNotification)
- .takeUntil(self.rx.deallocated) //页面销毁自动移除通知监听
- .subscribe(onNext: { [weak self] notification in
- let userInfo = notification.userInfo
- // 动画的持续时间
- self?.duration = (userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double)!
- // 键盘的frame
- let keyboardFrame = userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
- self?.keyboardH = (keyboardFrame?.size.height)!
- })
-
- //监听键盘隐藏通知
- _ = NotificationCenter.default.rx
- .notification(UIResponder.keyboardWillHideNotification)
- .takeUntil(self.rx.deallocated) //页面销毁自动移除通知监听
- .subscribe(onNext: { [weak self] notification in
- let userInfo = notification.userInfo
- // 动画的持续时间
- self?.duration = (userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double)!
- // 键盘的frame
- let keyboardFrame = userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
- self?.keyboardH = (keyboardFrame?.size.height)!
- self?.dismisskeyBoardCommentView()
- })
-
- //发送
- sendButton.rx.tap.subscribe(onNext: {
- [weak self] _ in
- self?.sendClosure?(self?.inputTextView.text ?? "")
- }).disposed(by: disposeBag)
- }
-
-
- /// 发送完成回调
- typealias SendClosure = (String) -> ()?
- var sendClosure :SendClosure?
-
-
- /// 隐藏View回调
- typealias HiddenViewClosure = () -> ()
- var hiddenViewClosure :HiddenViewClosure?
-
-
- /// 显示View
- func showkeyBoardCommentView() {
- self.inputTextView.becomeFirstResponder()
- }
-
- /// 隐藏View
- func dismisskeyBoardCommentView() {
- self.hide {
- [weak self] _ in
- if let hiddenViewClosure = self?.hiddenViewClosure {
- hiddenViewClosure()
- }
- }
- }
-
- /// 初始化
- class func keyBoardCommentView(showView:UIView? = nil, placeholder:String = "添加评论...",sendClosure :SendClosure? = nil) -> KeyBoardCommentView {
- let view = KeyBoardCommentView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 48))
- view.showkeyBoardCommentView()
- if showView != nil{
- view.attachedView = showView
- view.showView = showView
- }
- view.withKeyboard = true
- view.inputTextView.placeholder = placeholder
- if sendClosure != nil {
- view.sendClosure = sendClosure
- }
- let vProperty = FWPopupViewProperty()
- vProperty.animationDuration = view.duration
- vProperty.popupCustomAlignment = .bottomCenter
- view.frame = CGRect(x: 0, y: kScaleHeight - view.keyboardH , width: kScreenWidth, height: 48)
- vProperty.popupViewEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
- vProperty.popupAnimationType = .frame
- vProperty.maskViewColor = UIColor(white: 0, alpha: 0.5)
- vProperty.touchWildToHide = "1"
- view.vProperty = vProperty
- view.show()
- view.superview?.bringSubviewToFront(view)
- return view
- }
- }
- extension KeyBoardCommentView : UITextViewDelegate {
-
- /// 改变textView的高度
- func textViewDidChange(_ textView: UITextView) {
- TextLimitTool.restrictionInputTextView(inputTextView, maxNumber: 150)
- if inputTextView.text.count > 0 {
- sendButton.isEnabled = true
- }else {
- sendButton.isEnabled = false
- }
- let contentSizeH = self.inputTextView.contentSize.height
- if (contentSizeH > 28) {
- textView.height = contentSizeH
- }else {
- textView.height = 28
- }
- self.height = textView.height + 20
- if showView != nil {
- self.y = (attachedView?.height ?? 0) - keyboardH - self.height
- }else {
- self.y = kScreenHeight - self.height
- }
- sendButton.y = textView.height + 20 - 38
- self.frame = CGRect(x: 0, y: self.y , width: kScreenWidth, height: self.height)
- }
- }
|