123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- //
- // CommentInputView.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2019/7/8.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- //
- import UIKit
- import IQKeyboardManagerSwift
- import RxSwift
- class CommentInputView: UIView {
-
- let disposeBag = DisposeBag()
- /// 上下间距
- let topOrBottomEdge : CGFloat = 10
- /// 输入框宽度
- let inputTextViewWidth = kScreenWidth - 14 - 89
- /// 键盘的Y
- var keyboardH : CGFloat = 0
- /// 是否隐藏View
- var inputViewIsHidden : Bool = false {
- didSet {
-
- }
- }
- /// 添加视图的高度
- var screenHeight : CGFloat = 0
-
-
- typealias CommentInputViewClosure = (String) -> ()
- var commentInputViewClosure :CommentInputViewClosure?
-
- override init(frame: CGRect) {
- super.init(frame: frame)
- }
-
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- /// 初始化是否隐藏
- init(isHidden:Bool,height:CGFloat = kScreenHeight) {
- self.init()
- inputViewIsHidden = isHidden
- screenHeight = height
- if isHidden {
- self.frame = CGRect(x: 0, y: screenHeight, width: kScreenWidth, height: kSafeTabBarHeight+48)
- } else {
- self.frame = CGRect(x: 0, y: screenHeight-kSafeTabBarHeight-48, width: kScreenWidth, height: kSafeTabBarHeight+48)
- }
- setupViews()
- setupData()
- }
-
- func setupViews() {
- self.backgroundColor = UIColor.white
- addSubview(inputTextView)
- addSubview(sendButton)
- }
- lazy var inputTextView: IQTextView = {
- let inputTextView = IQTextView(frame: CGRect(x: 14, y: topOrBottomEdge, width: inputTextViewWidth, height: 30))
- 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: 15)
- inputTextView.textContainer.lineFragmentPadding = 0
- inputTextView.layer.cornerRadius = 14
- inputTextView.layer.masksToBounds = true
- inputTextView.placeholder = "添加评论...";
- 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 = false
- return sendButton
- }()
-
- func setupData() {
- //监听键盘弹出通知
- _ = NotificationCenter.default.rx
- .notification(UIResponder.keyboardWillShowNotification)
- .takeUntil(self.rx.deallocated) //页面销毁自动移除通知监听
- .subscribe(onNext: { [weak self] notification in
- let userInfo = notification.userInfo
- // 动画的持续时间
- let duration = userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double
- // 键盘的frame
- let keyboardFrame = userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
- self?.keyboardH = (keyboardFrame?.size.height)!
- if self?.inputTextView.text.count ?? 0 > 0 {
- self?.sendButton.isEnabled = true
- }else {
- self?.sendButton.isEnabled = false
- }
- UIView.animate(withDuration: duration ?? 0, animations: {
- self?.y = (self?.screenHeight)! - (keyboardFrame?.size.height)! - (self?.height)! + kSafeTabBarHeight
- })
- })
-
-
-
- //监听键盘隐藏通知
- _ = NotificationCenter.default.rx
- .notification(UIResponder.keyboardWillHideNotification)
- .takeUntil(self.rx.deallocated) //页面销毁自动移除通知监听
- .subscribe(onNext: { [weak self] notification in
- let userInfo = notification.userInfo
- // 动画的持续时间
- let duration = userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double
- // 键盘的frame
- let keyboardFrame = userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
- self?.keyboardH = (keyboardFrame?.size.height)!
- self?.inputTextView.placeholder = "添加评论...";
- self?.sendButton.isEnabled = false
- if let keyboardWillHideNotificationClosure = self?.keyboardWillHideNotificationClosure {
- keyboardWillHideNotificationClosure()
- }
- UIView.animate(withDuration: duration ?? 0, animations: {
- if self?.inputViewIsHidden ?? true {
- self?.y = (self?.screenHeight)!
- }else {
- self?.y = (self?.screenHeight)! - (self?.height)!
-
- }
- })
- })
- //发送
- sendButton.rx.tap.subscribe(onNext: {[weak self] _ in
- if let commentInputViewClosure = self?.commentInputViewClosure {
- commentInputViewClosure(self?.inputTextView.text ?? "")
- }
- }).disposed(by: disposeBag)
- }
-
- /// 发送成功的回调
- func sendSuccess() {
- self.inputTextView.text = ""
- self.inputTextView.height = 30
- self.height = 48 + kSafeTabBarHeight
- self.y = screenHeight - self.height
- self.endEditing(true)
- }
-
- typealias KeyboardWillHideNotificationClosure = () -> Void
- var keyboardWillHideNotificationClosure : KeyboardWillHideNotificationClosure?
- }
- extension CommentInputView : UITextViewDelegate {
-
- 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 > 30) {
- textView.height = contentSizeH
- }else {
- textView.height = 30
- }
- self.height = (self.inputTextView.height) + kSafeTabBarHeight + 20
- self.y = screenHeight - (self.keyboardH) - (self.inputTextView.height) - 20
- }
- }
|