宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-06 a1ae6802080a22e6e6ce6d0935e95facb1daca5c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
//  CommonAlertSheetView.swift
//  OoProject
//
//  Created by 无故事王国 on 2021/10/25.
//
 
import UIKit
import RxSwift
 
enum AlertSheetType {
    case single
    case img
}
// MARK: -- 【需封装此类】
class CommonAlertSheetView: UIView,LDNibView {
 
    @IBOutlet weak var centerView: UIView!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var cancelHeiCons: NSLayoutConstraint!
    @IBOutlet weak var contentHeiCons: NSLayoutConstraint!
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var completeBtn: UIButton!
 
    
    private var handlerVC:UIViewController?
    private var titles = [String]()
    private var imgs:[String]? = nil
    private var clouse:((Int,String)->Void)?
    private var disposeBag = DisposeBag()
    private var maxHei:CGFloat?
    private var mutableSelect:Bool = false
    private var mutableSelectIndex = Set<IndexPath>()
 
    var mutableSlectClouse:(([Int],[String])->Void)?
 
    override func awakeFromNib() {
        super.awakeFromNib()
        cancelHeiCons.constant = UIDevice.ld_safeEdges.bottom + 56
        tableView.register(cellName: "CommonSheetTCell", identifier: "_CommonSheetTCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.isScrollEnabled = false
        tableView.separatorStyle = .none
    }
 
    override func layoutSubviews() {
        super.layoutSubviews()
        centerView.ld_cornerPartWithNib(byRoundingCorners: [.topLeft,.topRight], radii: 12, size: centerView.size)
    }
 
    @discardableResult
    static func show(vc:UIViewController? = nil,type:AlertSheetType,titles:[String],imgs:[String]? = nil,maxHei:CGFloat? = nil,mutableSelect:Bool  = false,clouse:@escaping (Int,String)->Void,mutableClouse:(([Int],[String])->Void)? = nil)->CommonAlertSheetView{
        let sheetView = CommonAlertSheetView.ld_loadNibView()
        sheetView.handlerVC = vc
        sheetView.titles = titles
        sheetView.imgs = imgs
        sheetView.alpha = 0
        sheetView.clouse = clouse
        sheetView.mutableSlectClouse = mutableClouse
        sheetView.maxHei = maxHei
        sheetView.completeBtn.isHidden = !mutableSelect
        sheetView.mutableSelect = mutableSelect
        app.window?.addSubview(sheetView)
        sheetView.frame = app.window?.frame ?? CGRect.zero
        if let maxH = maxHei,maxH < CGFloat(titles.count) * 56.0 {
            sheetView.contentHeiCons.constant = maxH
            sheetView.tableView.isScrollEnabled = true
        }else{
            sheetView.contentHeiCons.constant = CGFloat(titles.count) * 56.0
        }
 
        UIView.animate(withDuration: 0.4) {
            sheetView.alpha = 1.0
            sheetView.layoutIfNeeded()
        }
        return sheetView
    }
 
    @discardableResult
    static func showWithNoCancel(vc:UIViewController? = nil,type:AlertSheetType,titles:[String],imgs:[String]? = nil,maxHei:CGFloat? = nil,mutableSelect:Bool  = false,clouse:@escaping (Int,String)->Void,mutableClouse:(([Int],[String])->Void)? = nil)->CommonAlertSheetView{
        let sheetView = CommonAlertSheetView.ld_loadNibView()
        sheetView.handlerVC = vc
        sheetView.titles = titles
        sheetView.imgs = imgs
        sheetView.alpha = 0
        sheetView.clouse = clouse
        sheetView.mutableSlectClouse = mutableClouse
        sheetView.maxHei = maxHei
        sheetView.completeBtn.isHidden = !mutableSelect
        sheetView.mutableSelect = mutableSelect
        app.window?.addSubview(sheetView)
        sheetView.frame = app.window?.frame ?? CGRect.zero
        sheetView.cancelBtn.setTitle("", for: .normal)
        sheetView.cancelHeiCons.constant = UIDevice.ld_safeEdges.bottom
 
        if let maxH = maxHei,maxH < CGFloat(titles.count) * 56.0 {
            sheetView.contentHeiCons.constant = maxH
            sheetView.tableView.isScrollEnabled = true
        }else{
            sheetView.contentHeiCons.constant = CGFloat(titles.count) * 56.0
        }
        
        UIView.animate(withDuration: 0.4) {
            sheetView.alpha = 1.0
            sheetView.layoutIfNeeded()
        }
        return sheetView
    }
    
    @IBAction func cancelAction(_ sender: UIButton) {
        UIView.animate(withDuration: 0.4) {
            self.alpha = 0
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
 
    @IBAction func completeAction(_ sender: UIButton) {
        var title = [String]()
        for item in mutableSelectIndex {
            title.append(titles[item.row])
        }
        mutableSlectClouse?(mutableSelectIndex.map({$0.row}),title)
        UIView.animate(withDuration: 0.4) {
            self.alpha = 0
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
}
 
extension CommonAlertSheetView:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if mutableSelect{
            if mutableSelectIndex.contains(indexPath){
                mutableSelectIndex.remove(indexPath)
            }else{
                mutableSelectIndex.insert(indexPath)
            }
            tableView.reloadData()
        }
    }
}
 
 
extension CommonAlertSheetView:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "_CommonSheetTCell") as! CommonSheetTCell
        cell.btn.setTitle(titles[indexPath.row], for: .normal)
        if imgs != nil{
            cell.btn.setImage(UIImage(named: imgs![indexPath.row]), for: .normal)
            cell.btn.imagePosition = .left 
            cell.btn.spacingBetweenImageAndTitle = 6
        }
 
        cell.btn.isUserInteractionEnabled = !mutableSelect
 
        if !mutableSelect{
            cell.btn.rx.tap.subscribe { [weak self]_ in
                self?.clouse?(indexPath.row,self?.titles[indexPath.row] ?? "")
                UIView.animate(withDuration: 0.4) {
                    self?.alpha = 0
                } completion: { _ in
                    self?.removeFromSuperview()
                }
            }.disposed(by: disposeBag)
        }
 
        if mutableSelect{
            cell.selectImg.isHidden = !mutableSelectIndex.contains(indexPath)
        }else{
            cell.selectImg.isHidden = true
        }
 
        return cell
    }
}