|
@@ -7,253 +7,162 @@
|
|
|
//
|
|
|
|
|
|
import Foundation
|
|
|
-import IQKeyboardManagerSwift
|
|
|
+import InputBarAccessoryView
|
|
|
|
|
|
typealias InputViewResultClosure = (_ contentStr: String) -> Void
|
|
|
|
|
|
-typealias DismissClosure = () -> Void
|
|
|
+typealias InputViewDismissClosure = () -> Void
|
|
|
|
|
|
typealias InputAccessoryConfigeClosure = (_ inputConfiger: InputConfiger) -> Void
|
|
|
|
|
|
-class KeyBoardInputView: NSObject {
|
|
|
-
|
|
|
- deinit {
|
|
|
- NXLLog("deinit")
|
|
|
- }
|
|
|
+class KeyBoardInputView: InputBarAccessoryView {
|
|
|
|
|
|
private var inputViewResultClosure : InputViewResultClosure!
|
|
|
- private var dismissClosure : DismissClosure!
|
|
|
+ private var inputViewDismissClosure : InputViewDismissClosure!
|
|
|
|
|
|
- private var inputView : IQTextView!
|
|
|
- private var btnSave : UIButton!
|
|
|
- private var toolbar : UIView!
|
|
|
|
|
|
- private var assistDismissButton : UIButton!
|
|
|
- private var assistInputView : UITextField!
|
|
|
+ private lazy var assistDismissButton: UIButton = {
|
|
|
+ let assistDismissButton = UIButton(type: UIButton.ButtonType.custom)
|
|
|
+ assistDismissButton.backgroundColor = UIColor(white: 0, alpha: 0.5)
|
|
|
+ assistDismissButton.addTarget(self, action: #selector(dismiss), for: UIControl.Event.touchUpInside)
|
|
|
+ return assistDismissButton
|
|
|
+ }()
|
|
|
|
|
|
- static let shared : KeyBoardInputView = KeyBoardInputView()
|
|
|
-
|
|
|
- class func dimiss() {
|
|
|
- KeyBoardInputView.shared.dismiss()
|
|
|
- }
|
|
|
+ private lazy var keyboardManager: KeyboardManager = {
|
|
|
+ let keyboardManager = KeyboardManager()
|
|
|
+ return keyboardManager
|
|
|
+ }()
|
|
|
|
|
|
- class func show(inputViewResultClosure:@escaping InputViewResultClosure,dismissClosure:@escaping DismissClosure) {
|
|
|
- KeyBoardInputView.shared.initInputView(inputConfiger: InputConfiger(), inputViewResultClosure: inputViewResultClosure, dismissClosure: dismissClosure)
|
|
|
- }
|
|
|
+ private var keyBoardInputView : KeyBoardInputView?
|
|
|
|
|
|
- class func show(KeyboardType:UIKeyboardType,inputViewResultClosure:@escaping InputViewResultClosure,dismissClosure:@escaping DismissClosure) {
|
|
|
- let inputConfiger = InputConfiger()
|
|
|
- inputConfiger.keyboardType = KeyboardType
|
|
|
- KeyBoardInputView.shared.initInputView(inputConfiger: inputConfiger, inputViewResultClosure: inputViewResultClosure, dismissClosure: dismissClosure)
|
|
|
-
|
|
|
+ class func show(inputViewResultClosure:@escaping InputViewResultClosure,inputViewDismissClosure:@escaping InputViewDismissClosure) {
|
|
|
+ KeyBoardInputView.initInputView(inputConfiger: InputConfiger(), inputViewResultClosure: inputViewResultClosure, inputViewDismissClosure: inputViewDismissClosure)
|
|
|
}
|
|
|
|
|
|
- class func show(KeyboardType:UIKeyboardType,content:String,inputViewResultClosure:@escaping InputViewResultClosure,dismissClosure:@escaping DismissClosure) {
|
|
|
+ class func show(placeholder:String,inputViewResultClosure:@escaping InputViewResultClosure,inputViewDismissClosure:@escaping InputViewDismissClosure) {
|
|
|
let inputConfiger = InputConfiger()
|
|
|
- inputConfiger.keyboardType = KeyboardType
|
|
|
- inputConfiger.content = content
|
|
|
- KeyBoardInputView.shared.initInputView(inputConfiger: inputConfiger, inputViewResultClosure: inputViewResultClosure, dismissClosure: dismissClosure)
|
|
|
+ inputConfiger.placeholder = placeholder
|
|
|
+ KeyBoardInputView.initInputView(inputConfiger: inputConfiger, inputViewResultClosure: inputViewResultClosure, inputViewDismissClosure: inputViewDismissClosure)
|
|
|
}
|
|
|
|
|
|
- class func show(inputAccessoryConfigeClosure:@escaping InputAccessoryConfigeClosure,inputViewResultClosure:@escaping InputViewResultClosure,dismissClosure:@escaping DismissClosure) {
|
|
|
+ class func show(inputAccessoryConfigeClosure:@escaping InputAccessoryConfigeClosure,inputViewResultClosure:@escaping InputViewResultClosure,inputViewDismissClosure:@escaping InputViewDismissClosure) {
|
|
|
|
|
|
let inputConfiger = InputConfiger()
|
|
|
inputAccessoryConfigeClosure(inputConfiger)
|
|
|
- KeyBoardInputView.shared.initInputView(inputConfiger: inputConfiger, inputViewResultClosure: inputViewResultClosure, dismissClosure: dismissClosure)
|
|
|
+ KeyBoardInputView.initInputView(inputConfiger: inputConfiger, inputViewResultClosure: inputViewResultClosure, inputViewDismissClosure: inputViewDismissClosure)
|
|
|
}
|
|
|
|
|
|
- private func initInputView(inputConfiger:InputConfiger,inputViewResultClosure:@escaping InputViewResultClosure,dismissClosure:@escaping DismissClosure) {
|
|
|
+ private class func initInputView(inputConfiger:InputConfiger,inputViewResultClosure:@escaping InputViewResultClosure,inputViewDismissClosure:@escaping InputViewDismissClosure) {
|
|
|
+ let keyBoardInputView = KeyBoardInputView()
|
|
|
+ keyBoardInputView.assistDismissButton.backgroundColor = inputConfiger.backgroundColor
|
|
|
+ keyBoardInputView.inputTextView.placeholder = inputConfiger.placeholder
|
|
|
+ keyBoardInputView.keyBoardInputView = keyBoardInputView
|
|
|
+ keyBoardInputView.inputViewResultClosure = inputViewResultClosure
|
|
|
+ keyBoardInputView.inputViewDismissClosure = inputViewDismissClosure
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ override init(frame: CGRect) {
|
|
|
+ super.init(frame: frame)
|
|
|
initView()
|
|
|
+ }
|
|
|
+
|
|
|
+ required init?(coder aDecoder: NSCoder) {
|
|
|
+ fatalError("init(coder:) has not been implemented")
|
|
|
+ }
|
|
|
+
|
|
|
+ private func initView() {
|
|
|
let window = kAppDelegate.window
|
|
|
- self.assistDismissButton.frame = window!.bounds
|
|
|
- self.assistDismissButton.autoresizingMask = UIView.AutoresizingMask(rawValue: UIView.AutoresizingMask.flexibleWidth.rawValue | UIView.AutoresizingMask.flexibleHeight.rawValue)
|
|
|
- window?.addSubview(assistDismissButton)
|
|
|
-
|
|
|
- self.updateLayout(beigin: true)
|
|
|
-
|
|
|
- self.inputViewResultClosure = inputViewResultClosure
|
|
|
- self.dismissClosure = dismissClosure
|
|
|
- self.inputView.text = inputConfiger.content
|
|
|
- self.assistInputView.text = inputConfiger.content
|
|
|
-
|
|
|
- self.inputView.keyboardType = inputConfiger.keyboardType;
|
|
|
- self.inputView.placeholder = inputConfiger.placeholder
|
|
|
- self.assistInputView.keyboardType = inputConfiger.keyboardType;
|
|
|
- self.btnSave.isEnabled = false
|
|
|
- self.assistDismissButton.isUserInteractionEnabled = inputConfiger.backgroundUserInterface
|
|
|
- self.assistDismissButton.backgroundColor = inputConfiger.backgroundColor
|
|
|
+ self.backgroundColor = UIColor.clear
|
|
|
+ self.backgroundView.backgroundColor = UIColor.clear
|
|
|
|
|
|
- self.assistInputView.inputAccessoryView = toolbar
|
|
|
- self.assistInputView.becomeFirstResponder()
|
|
|
-
|
|
|
- NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidShowNotification, object: nil, queue: nil) {[weak self] (notification) in
|
|
|
- if ((self?.assistDismissButton?.window) != nil) && (self?.assistInputView.isFirstResponder)! {
|
|
|
- self?.inputView.becomeFirstResponder()
|
|
|
- self?.inputView.scrollRangeToVisible(NSRange(location: (self?.inputView.text.count)!, length: 1))
|
|
|
- }
|
|
|
- }
|
|
|
+ assistDismissButton.frame = window!.bounds
|
|
|
+ assistDismissButton.autoresizingMask = UIView.AutoresizingMask(rawValue: UIView.AutoresizingMask.flexibleWidth.rawValue | UIView.AutoresizingMask.flexibleHeight.rawValue)
|
|
|
+ window?.addSubview(assistDismissButton)
|
|
|
|
|
|
- //输入切换
|
|
|
- NotificationCenter.default.addObserver(forName: UITextInputMode.currentInputModeDidChangeNotification, object: nil, queue: nil) {
|
|
|
- [weak self] (notification) in
|
|
|
- self?.assistInputView.resignFirstResponder()
|
|
|
- self?.assistInputView.becomeFirstResponder()
|
|
|
+ frame = CGRect(x: 0, y: kScreenHeight, width: kScreenWidth, height: 48)
|
|
|
+ let imageView = UIImageView(frame: frame)
|
|
|
+ let image = UIImage(named: "comments_pic_bg")?
|
|
|
+ .resizableImage(withCapInsets: UIEdgeInsets(top: 10, left: 10, bottom: 0, right: 10),
|
|
|
+ resizingMode: .stretch) //左右15像素的部分不变,中间部分来拉伸
|
|
|
+ imageView.image = image
|
|
|
+ assistDismissButton.addSubview(imageView)
|
|
|
+ assistDismissButton.addSubview(self)
|
|
|
+
|
|
|
+ imageView.snp.makeConstraints { (make) in
|
|
|
+ make.edges.equalTo(self)
|
|
|
}
|
|
|
|
|
|
- NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil) { [weak self] (notification) in
|
|
|
- if (self?.inputView.isFirstResponder)! {
|
|
|
- self?.dismiss()
|
|
|
+ delegate = self
|
|
|
+
|
|
|
+ inputTextView.tintColor = kThemeColor
|
|
|
+ inputTextView.backgroundColor = kfafafaColor
|
|
|
+ inputTextView.cornerRadius = 14
|
|
|
+ inputTextView.masksToBounds = true
|
|
|
+ inputTextView.layer.borderColor = kf0f0f0Color.cgColor
|
|
|
+ inputTextView.layer.borderWidth = 0.5
|
|
|
+ inputTextView.placeholderTextColor = k999999Color
|
|
|
+ inputTextView.textContainerInset = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
|
|
|
+ inputTextView.placeholderLabelInsets = UIEdgeInsets(top: 5, left: 15, bottom: 5, right: 15)
|
|
|
+
|
|
|
+ sendButton.setBackgroundImage(UIImage.imageWithColor(color: kd8d8d8Color), for: UIControl.State.disabled)
|
|
|
+ sendButton.setBackgroundImage(UIImage.imageWithColor(color: kThemeColor), for: UIControl.State.normal)
|
|
|
+ sendButton.titleLabel?.font = kRegularFont14
|
|
|
+ sendButton.setTitleColor(UIColor.white, for: UIControl.State.normal)
|
|
|
+ sendButton.setTitleColor(UIColor.white, for: UIControl.State.disabled)
|
|
|
+ sendButton.setSize(CGSize(width: 60, height: 28), animated: false)
|
|
|
+ sendButton.title = "发布"
|
|
|
+ sendButton.cornerRadius = 14
|
|
|
+ sendButton.masksToBounds = true
|
|
|
+
|
|
|
+ padding = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
|
|
|
+ middleContentViewPadding = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 10)
|
|
|
+ separatorLine.isHidden = true
|
|
|
+ setMiddleContentView(inputTextView, animated: false)
|
|
|
+
|
|
|
+ keyboardManager.bind(inputAccessoryView: self)
|
|
|
+ keyboardManager.on(event: KeyboardEvent.willHide) {
|
|
|
+ [weak self] (KeyboardNotification) in
|
|
|
+ if !(self?.sendButton.isEnabled ?? false) {
|
|
|
+ self?.cleanView()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidHideNotification, object: nil, queue: nil) { [weak self] (notification) in
|
|
|
- self?.cleanInitView()
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private func initView() {
|
|
|
-
|
|
|
- /// 背景button
|
|
|
- let backgroundBtn = UIButton(type: UIButton.ButtonType.custom)
|
|
|
- backgroundBtn.addTarget(self, action: #selector(dismiss), for: UIControl.Event.touchUpInside)
|
|
|
-
|
|
|
- /// 工具bar
|
|
|
- let toolbar = UIView()
|
|
|
- toolbar.backgroundColor = UIColor.white
|
|
|
-
|
|
|
-
|
|
|
- /// 输入框
|
|
|
- let textView = IQTextView()
|
|
|
- textView.returnKeyType = .done
|
|
|
- textView.enablesReturnKeyAutomatically = true
|
|
|
- textView.delegate = self
|
|
|
- textView.font = kRegularFont14
|
|
|
- textView.layer.borderColor = kf0f0f0Color.cgColor
|
|
|
- textView.layer.borderWidth = 0.5/UIScreen.main.scale
|
|
|
- textView.textContainerInset = UIEdgeInsets(top: 5.5, left: 15, bottom: 5.5, right: 15)
|
|
|
-// textView.textContainer.lineFragmentPadding = 0
|
|
|
- textView.cornerRadius = 14
|
|
|
- textView.masksToBounds = true
|
|
|
- textView.tintColor = kThemeColor
|
|
|
- textView.backgroundColor = kfafafaColor
|
|
|
- toolbar.addSubview(textView)
|
|
|
-
|
|
|
- let assistTxf = UITextField()
|
|
|
- assistTxf.returnKeyType = .done
|
|
|
- assistTxf.enablesReturnKeyAutomatically = true
|
|
|
- backgroundBtn.addSubview(assistTxf)
|
|
|
-
|
|
|
- /// 确定
|
|
|
- let saveBtn = UIButton(type: UIButton.ButtonType.custom)
|
|
|
- saveBtn.setBackgroundImage(UIImage.imageWithColor(color: kThemeColor), for: UIControl.State.normal)
|
|
|
- saveBtn.setBackgroundImage(UIImage.imageWithColor(color: kd8d8d8Color), for: UIControl.State.disabled)
|
|
|
- saveBtn.titleLabel?.font = kRegularFont14
|
|
|
- saveBtn.setTitle("发布", for: UIControl.State.normal)
|
|
|
- saveBtn.cornerRadius = 14
|
|
|
- saveBtn.masksToBounds = true
|
|
|
- saveBtn.setTitleColor(kffffffColor, for: UIControl.State.normal)
|
|
|
- saveBtn.addTarget(self, action: #selector(save), for: UIControl.Event.touchUpInside)
|
|
|
- toolbar.addSubview(saveBtn)
|
|
|
-
|
|
|
- self.toolbar = toolbar
|
|
|
- self.assistDismissButton = backgroundBtn
|
|
|
- self.inputView = textView
|
|
|
- self.assistInputView = assistTxf
|
|
|
- self.btnSave = saveBtn
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- func updateLayout(beigin:Bool) {
|
|
|
- let saveButtonWidth : CGFloat = 60
|
|
|
- let edge : CGFloat = 10.0
|
|
|
-
|
|
|
- var height = max(48, inputView.contentSize.height + 17.5)
|
|
|
- height = min(height, 100)
|
|
|
- UIView.animate(withDuration: beigin ? 0 : 0.2) {
|
|
|
- [weak self] in
|
|
|
- self?.toolbar.frame = CGRect(x: 0, y: 48-height, width: kScreenWidth, height: height)
|
|
|
- self?.toolbar.configRectCorner(corner: [.topLeft,.topRight], radii: CGSize(width: 8, height: 8))
|
|
|
- self?.inputView.frame = CGRect(x: 14, y: edge, width: kScreenWidth - saveButtonWidth - 38.0, height: height - edge * 2)
|
|
|
- self?.btnSave.frame = CGRect(x: kScreenWidth - saveButtonWidth - 14, y: height - 10 - 28 , width: saveButtonWidth, height: 28.0)
|
|
|
- self?.assistInputView.inputAccessoryView = self?.toolbar
|
|
|
- }
|
|
|
+ inputTextView.becomeFirstResponder()
|
|
|
}
|
|
|
|
|
|
@objc func dismiss() {
|
|
|
- self.inputView.resignFirstResponder()
|
|
|
- self.assistDismissButton.removeFromSuperview()
|
|
|
- NotificationCenter.default.removeObserver(self)
|
|
|
- }
|
|
|
-
|
|
|
- @objc func save() {
|
|
|
- self.inputView.resignFirstResponder()
|
|
|
- if self.btnSave.isEnabled {
|
|
|
- if let inputViewResultClosure = self.inputViewResultClosure {
|
|
|
- inputViewResultClosure(self.inputView.text ?? "")
|
|
|
- }
|
|
|
+ if let dismissClosure = self.inputViewDismissClosure {
|
|
|
+ dismissClosure()
|
|
|
}
|
|
|
- self.dismiss()
|
|
|
+ cleanView()
|
|
|
}
|
|
|
|
|
|
- func cleanInitView() {
|
|
|
-
|
|
|
- if let dismissClosure = self.dismissClosure {
|
|
|
- dismissClosure()
|
|
|
- }
|
|
|
- if self.toolbar != nil {
|
|
|
- self.toolbar.removeFromSuperview()
|
|
|
- }
|
|
|
- if self.assistDismissButton != nil {
|
|
|
- self.assistDismissButton.removeFromSuperview()
|
|
|
- }
|
|
|
- if self.inputView != nil {
|
|
|
- self.inputView.removeFromSuperview()
|
|
|
- }
|
|
|
- if self.assistInputView != nil {
|
|
|
- self.assistInputView.removeFromSuperview()
|
|
|
- }
|
|
|
- if self.btnSave != nil {
|
|
|
- self.btnSave.removeFromSuperview()
|
|
|
- }
|
|
|
- self.toolbar = nil
|
|
|
- self.assistDismissButton = nil
|
|
|
- self.inputView = nil
|
|
|
- self.assistInputView = nil
|
|
|
- self.btnSave = nil
|
|
|
- self.inputViewResultClosure = nil
|
|
|
+ private func cleanView() {
|
|
|
+ inputTextView.resignFirstResponder()
|
|
|
+ assistDismissButton.removeFromSuperview()
|
|
|
+ keyBoardInputView = nil
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
-extension KeyBoardInputView : UITextViewDelegate {
|
|
|
- func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
|
|
|
- if text == "\n" {
|
|
|
- self.save()
|
|
|
- return false
|
|
|
+extension KeyBoardInputView : InputBarAccessoryViewDelegate {
|
|
|
+
|
|
|
+ func inputBar(_ inputBar: InputBarAccessoryView, didPressSendButtonWith text: String) {
|
|
|
+ if let inputViewResultClosure = inputViewResultClosure {
|
|
|
+ inputViewResultClosure(text)
|
|
|
}
|
|
|
- return true
|
|
|
+ cleanView()
|
|
|
}
|
|
|
|
|
|
- func textViewDidChange(_ textView: UITextView) {
|
|
|
- TextLimitTool.restrictionInputTextView(textView, maxNumber: 150)
|
|
|
- self.updateLayout(beigin: false)
|
|
|
- if textView.text.count > 0 {
|
|
|
- self.btnSave.isEnabled = true
|
|
|
- }else {
|
|
|
- self.btnSave.isEnabled = false
|
|
|
- }
|
|
|
+ func inputBar(_ inputBar: InputBarAccessoryView, textViewTextDidChangeTo text: String) {
|
|
|
+ TextLimitTool.restrictionInputTextView(inputTextView, maxNumber: 150)
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
class InputConfiger: NSObject {
|
|
|
|
|
|
- // default UIKeyboardTypeDefault
|
|
|
- var keyboardType : UIKeyboardType = UIKeyboardType.default
|
|
|
- // default nil
|
|
|
- var content : String = ""
|
|
|
- // default YES
|
|
|
- var backgroundUserInterface : Bool = true
|
|
|
// default clearColor
|
|
|
var backgroundColor : UIColor = UIColor(white: 0, alpha: 0.5)
|
|
|
// default placeholder
|