杨锴
2024-11-05 0fb7413df54760ac6bd15b90b738e0706de1629e
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
//  CommonAlertSheetView.swift
//  WanPai
//
//  Created by 无故事王国 on 2023/10/9.
//
 
import UIKit
import JQTools
 
class CommonAlertSheetView: UIView,JQNibView {
 
                @IBOutlet weak var view_container: UIView!
                @IBOutlet weak var tableView: UITableView!
                @IBOutlet weak var cons_tableHei: NSLayoutConstraint!
                @IBOutlet weak var cons_viewBottom: NSLayoutConstraint!
    @IBOutlet weak var cons_titleH: NSLayoutConstraint!
    @IBOutlet weak var view_title: UIView!
    
                private var items = [String]()
                private var clouse:((Int)->Void)!
    private var hiddenTitle:Bool = false
 
                override func awakeFromNib() {
                                super.awakeFromNib()
                                alpha = 0
 
                                cons_viewBottom.constant = -JQ_ScreenW
                                layoutIfNeeded()
                                tableView.delegate = self
                                tableView.dataSource = self
                                tableView.separatorStyle = .none
        tableView.isScrollEnabled = false
                                tableView.register(CommonAlertSheetTCell.self, forCellReuseIdentifier: "TCell")
                }
 
    static func show(items:[String],hiddenTitle:Bool = false,clouse:@escaping (Int)->Void){
                                let alertView = CommonAlertSheetView.jq_loadNibView()
        alertView.hiddenTitle = hiddenTitle
        alertView.cons_titleH.constant = hiddenTitle ? 0:56
        alertView.view_title.isHidden = hiddenTitle
 
                                alertView.frame = sceneDelegate?.window?.frame ?? .zero
                                alertView.cons_tableHei.constant = CGFloat(items.count * 56)
                                sceneDelegate?.window?.addSubview(alertView)
                                alertView.cons_viewBottom.constant = 0
                                alertView.items = items
                                alertView.clouse = clouse
 
                                UIView.animate(withDuration: 0.4) {
                                                alertView.layoutIfNeeded()
                                                alertView.alpha = 1
                                } completion: { _ in
 
                                }
                }
 
                override func layoutSubviews() {
                                super.layoutSubviews()
        var totalH:Double = 0
        if hiddenTitle{
            totalH = Double(items.count * 56) + UIDevice.jq_safeEdges.bottom + 56.0
        }else{
            totalH = Double(items.count * 56) + UIDevice.jq_safeEdges.bottom + 56.0 + 56.0
        }
 
                                view_container.jq_addCorners(corner: [.topLeft,.topRight], radius: 20,height: totalH)
                }
 
                private func hidden(){
                                self.cons_viewBottom.constant = -JQ_ScreenW
                                UIView.animate(withDuration: 0.4) {
                                                self.alpha = 0
                                                self.layoutIfNeeded()
                                } completion: { _ in
                                                self.removeFromSuperview()
                                }
                }
 
                @IBAction func hiddenAction(_ sender: UIButton) {
                                hidden()
                }
}
 
extension CommonAlertSheetView:UITableViewDelegate{
                func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                                clouse(indexPath.row)
                                hidden()
                }
}
 
extension CommonAlertSheetView:UITableViewDataSource{
                func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                                return items.count
                }
 
                func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                                let cell = tableView.dequeueReusableCell(withIdentifier: "TCell") as! CommonAlertSheetTCell
                                cell.label_content.text = items[indexPath.row]
        if hiddenTitle{
            cell.label_content.textColor = UIColor(hexString: "#6B6B6B")
        }else{
            cell.label_content.textColor = UIColor(hexString: "#92A87D")
        }
                                return cell
                }
 
                func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
                                return 56
                }
}
 
fileprivate class CommonAlertSheetTCell:UITableViewCell{
 
                var label_content:UILabel!
 
                override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
                                super.init(style: style, reuseIdentifier: reuseIdentifier)
 
                                selectionStyle = .none
                                label_content = UILabel()
                                label_content.text = "123"
                                label_content.font = UIFont.systemFont(ofSize: 15, weight: .medium)
                                label_content.textColor = UIColor(hexString: "#6B6B6B")
 
                                contentView.addSubview(label_content)
                                label_content.snp.makeConstraints { make in
                                                make.center.equalToSuperview()
                                }
 
                                let lineView = UIView()
                                lineView.backgroundColor = UIColor(hexStr: "#979797").withAlphaComponent(0.1)
                                contentView.addSubview(lineView)
                                lineView.snp.makeConstraints { make in
                                                make.left.equalTo(15)
                                                make.right.equalTo(-15)
                                                make.height.equalTo(0.5)
                                                make.bottom.equalToSuperview()
                                }
                }
 
                required init?(coder: NSCoder) {
                                fatalError("init(coder:) has not been implemented")
                }
}