宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
//  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
    }
}