CommentInputView.swift 7.0 KB

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