KeyBoardCommentView.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // KeyBoardCommentView.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/7/11.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import IQKeyboardManagerSwift
  10. import RxSwift
  11. import FWPopupView
  12. class KeyBoardCommentView: FWPopupView {
  13. let disposeBag = DisposeBag()
  14. /// 上下间距
  15. let topOrBottomEdge : CGFloat = 10
  16. /// 输入框宽度
  17. let inputTextViewWidth = kScreenWidth - 14 - 84
  18. /// 键盘的高度
  19. var keyboardH : CGFloat = 0
  20. /// 动画时长
  21. var duration : Double = 0
  22. /// 显示的View
  23. var showView : UIView?
  24. override init(frame: CGRect) {
  25. super.init(frame: frame)
  26. setupViews()
  27. setupData()
  28. configRectCorner(corner: [.topLeft,.topRight], radii: CGSize(width: 8, height: 8))
  29. }
  30. required init?(coder aDecoder: NSCoder) {
  31. fatalError("init(coder:) has not been implemented")
  32. }
  33. func setupViews() {
  34. backgroundColor = UIColor.white
  35. addSubview(inputTextView)
  36. addSubview(sendButton)
  37. }
  38. lazy var inputTextView: IQTextView = {
  39. let inputTextView = IQTextView(frame: CGRect(x: 14, y: topOrBottomEdge, width: inputTextViewWidth, height: 28))
  40. inputTextView.backgroundColor = kf0f0f0Color
  41. inputTextView.font = kRegularFont14
  42. inputTextView.layoutManager.allowsNonContiguousLayout = false
  43. inputTextView.enablesReturnKeyAutomatically = true
  44. inputTextView.scrollsToTop = false
  45. inputTextView.textContainerInset = UIEdgeInsets(top: 5, left: 15, bottom: 5, right: 10)
  46. inputTextView.textContainer.lineFragmentPadding = 0
  47. inputTextView.layer.cornerRadius = 14
  48. inputTextView.layer.masksToBounds = true
  49. inputTextView.delegate = self
  50. inputTextView.tintColor = kThemeColor
  51. return inputTextView
  52. }()
  53. lazy var sendButton: UIButton = {
  54. let sendButton = UIButton(type: UIButton.ButtonType.custom)
  55. sendButton.frame = CGRect(x: kScreenWidth - 14 - 60, y: 11, width: 60, height: 28)
  56. sendButton.setTitle("发送", for: UIControl.State.normal)
  57. sendButton.setBackgroundImage(UIImage.imageWithColor(color: kThemeColor), for: UIControl.State.normal)
  58. sendButton.setBackgroundImage(UIImage.imageWithColor(color: kd8d8d8Color), for: UIControl.State.disabled)
  59. sendButton.titleLabel?.font = kRegularFont14
  60. sendButton.layer.cornerRadius = 14;
  61. sendButton.layer.masksToBounds = true
  62. sendButton.isEnabled = true
  63. return sendButton
  64. }()
  65. func setupData() {
  66. //监听键盘弹出通知
  67. _ = NotificationCenter.default.rx
  68. .notification(UIResponder.keyboardWillShowNotification)
  69. .takeUntil(self.rx.deallocated) //页面销毁自动移除通知监听
  70. .subscribe(onNext: { [weak self] notification in
  71. let userInfo = notification.userInfo
  72. // 动画的持续时间
  73. self?.duration = (userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double)!
  74. // 键盘的frame
  75. let keyboardFrame = userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
  76. self?.keyboardH = (keyboardFrame?.size.height)!
  77. })
  78. //监听键盘隐藏通知
  79. _ = NotificationCenter.default.rx
  80. .notification(UIResponder.keyboardWillHideNotification)
  81. .takeUntil(self.rx.deallocated) //页面销毁自动移除通知监听
  82. .subscribe(onNext: { [weak self] notification in
  83. let userInfo = notification.userInfo
  84. // 动画的持续时间
  85. self?.duration = (userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double)!
  86. // 键盘的frame
  87. let keyboardFrame = userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
  88. self?.keyboardH = (keyboardFrame?.size.height)!
  89. self?.dismisskeyBoardCommentView()
  90. })
  91. //发送
  92. sendButton.rx.tap.subscribe(onNext: {
  93. [weak self] _ in
  94. self?.sendClosure?(self?.inputTextView.text ?? "")
  95. }).disposed(by: disposeBag)
  96. }
  97. /// 发送完成回调
  98. typealias SendClosure = (String) -> ()?
  99. var sendClosure :SendClosure?
  100. /// 隐藏View回调
  101. typealias HiddenViewClosure = () -> ()
  102. var hiddenViewClosure :HiddenViewClosure?
  103. /// 显示View
  104. func showkeyBoardCommentView() {
  105. self.inputTextView.becomeFirstResponder()
  106. }
  107. /// 隐藏View
  108. func dismisskeyBoardCommentView() {
  109. self.hide {
  110. [weak self] _ in
  111. if let hiddenViewClosure = self?.hiddenViewClosure {
  112. hiddenViewClosure()
  113. }
  114. }
  115. }
  116. /// 初始化
  117. class func keyBoardCommentView(showView:UIView? = nil, placeholder:String = "添加评论...",sendClosure :SendClosure? = nil) -> KeyBoardCommentView {
  118. let view = KeyBoardCommentView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 48))
  119. view.showkeyBoardCommentView()
  120. if showView != nil{
  121. view.attachedView = showView
  122. view.showView = showView
  123. }
  124. view.withKeyboard = true
  125. view.inputTextView.placeholder = placeholder
  126. if sendClosure != nil {
  127. view.sendClosure = sendClosure
  128. }
  129. let vProperty = FWPopupViewProperty()
  130. vProperty.animationDuration = view.duration
  131. vProperty.popupCustomAlignment = .bottomCenter
  132. view.frame = CGRect(x: 0, y: kScaleHeight - view.keyboardH , width: kScreenWidth, height: 48)
  133. vProperty.popupViewEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  134. vProperty.popupAnimationType = .frame
  135. vProperty.maskViewColor = UIColor(white: 0, alpha: 0.5)
  136. vProperty.touchWildToHide = "1"
  137. view.vProperty = vProperty
  138. view.show()
  139. view.superview?.bringSubviewToFront(view)
  140. return view
  141. }
  142. }
  143. extension KeyBoardCommentView : UITextViewDelegate {
  144. /// 改变textView的高度
  145. func textViewDidChange(_ textView: UITextView) {
  146. TextLimitTool.restrictionInputTextView(inputTextView, maxNumber: 150)
  147. if inputTextView.text.count > 0 {
  148. sendButton.isEnabled = true
  149. }else {
  150. sendButton.isEnabled = false
  151. }
  152. let contentSizeH = self.inputTextView.contentSize.height
  153. if (contentSizeH > 28) {
  154. textView.height = contentSizeH
  155. }else {
  156. textView.height = 28
  157. }
  158. self.height = textView.height + 20
  159. if showView != nil {
  160. self.y = (attachedView?.height ?? 0) - keyboardH - self.height
  161. }else {
  162. self.y = kScreenHeight - self.height
  163. }
  164. sendButton.y = textView.height + 20 - 38
  165. self.frame = CGRect(x: 0, y: self.y , width: kScreenWidth, height: self.height)
  166. }
  167. }