//
|
// PublishCommentView.swift
|
// OKProject
|
//
|
// Created by 无故事王国 on 2022/5/11.
|
// Copyright © 2022 yangwang. All rights reserved.
|
//
|
|
import UIKit
|
import QMUIKit
|
import RxCocoa
|
|
class PublishCommentView: UIView,LDNibView{
|
|
@IBOutlet weak var containerBottomCons: NSLayoutConstraint!
|
@IBOutlet weak var containerView: UIView!
|
@IBOutlet weak var textView: QMUITextView!
|
private var completeClouse:((String)->Void)?
|
|
override func awakeFromNib() {
|
super.awakeFromNib()
|
alpha = 0
|
containerBottomCons.constant = -159
|
textView.delegate = self
|
textView.returnKeyType = .send
|
setNeedsLayout()
|
|
NotificationCenter.default.rx.notification(UIApplication.keyboardWillShowNotification).subscribe(onNext: {noti in
|
if let userInfo = noti.userInfo{
|
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
|
let frame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
|
UIView.animate(withDuration: duration) {
|
self.alpha = 1
|
self.containerBottomCons.constant = frame.height
|
self.layoutIfNeeded()
|
}
|
}
|
}).disposed(by: rx.disposeBag)
|
|
NotificationCenter.default.rx.notification(UIApplication.keyboardWillHideNotification).subscribe(onNext: {noti in
|
if let userInfo = noti.userInfo{
|
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
|
UIView.animate(withDuration: duration) {
|
self.containerBottomCons.constant = 0
|
self.layoutIfNeeded()
|
}
|
}
|
}).disposed(by: rx.disposeBag)
|
}
|
|
@discardableResult
|
static func show(_ completeClouse:@escaping (String)->Void)->PublishCommentView{
|
let publishCommentView = PublishCommentView.ld_loadNibView()
|
app.window?.addSubview(publishCommentView)
|
publishCommentView.completeClouse = completeClouse
|
publishCommentView.frame = app.window?.frame ?? .zero
|
publishCommentView.textView.becomeFirstResponder()
|
return publishCommentView
|
}
|
|
private func hidden(_ duration:Double = 0.4){
|
UIView.animate(withDuration: 0.4) {
|
self.alpha = 0
|
self.containerBottomCons.constant = -159
|
self.layoutIfNeeded()
|
} completion: { _ in
|
self.removeFromSuperview()
|
}
|
}
|
|
@IBAction func closeAction(_ sender: UIButton) {
|
hidden()
|
}
|
|
@IBAction func sendAction(_ sender: UIButton) {
|
guard !textView.text.isEmpty else {
|
alert(text: "请输入内容");return
|
}
|
|
completeClouse?(textView.text)
|
hidden()
|
}
|
}
|
|
extension PublishCommentView:QMUITextViewDelegate{
|
func textViewShouldReturn(_ textView: QMUITextView!) -> Bool {
|
guard !textView.text.isEmpty else {
|
alert(text: "请输入内容");return false
|
}
|
completeClouse?(textView.text)
|
hidden()
|
return true
|
}
|
}
|