宽窄优行-由【嘉易行】项目成品而来
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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
//  CarPriceListView.swift
//  OKProject
//
//  Created by 无故事王国 on 2022/5/9.
//  Copyright © 2022 yangwang. All rights reserved.
//
 
import UIKit
import RxSwift
 
class CarPriceListView: UIView,LDNibView{
 
    private let cellW = (ScreenWidth - 76) / 4
    private let cellH = (ScreenWidth - 76) / 4 * 0.355
    private var selectIndex = 0
 
    @IBOutlet weak var itemCollectionView: UICollectionView!
    @IBOutlet weak var itemCollectionHeiCons: NSLayoutConstraint!
    @IBOutlet weak var minimumPriceField: UITextField!
    @IBOutlet weak var maximumPriceField: UITextField!
 
    private let items = ["价格不限","4万元以内","4-6万元","6-8万元","8万元以上"]
 
    private var hiddenClouse:(()->Void)?
    private var completeClouse:((Int,Double?,Double?)->Void)?
 
    private var maxPrice:Double?
    private var minPrice:Double?
    private var disposeBag = DisposeBag()
 
    override func awakeFromNib() {
        super.awakeFromNib()
        alpha = 0
        itemCollectionView.delegate = self
        itemCollectionView.dataSource = self
        itemCollectionView.contentInset = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
        itemCollectionView.register(UINib(nibName: "Common_SingleText_CCell", bundle: nil), forCellWithReuseIdentifier: "_Common_SingleText_CCell")
        layoutIfNeeded()
        calHei()
 
        minimumPriceField.rx.controlEvent(.editingDidBegin).subscribe(onNext: {[weak self]() in
            self?.selectIndex = -1
            self?.itemCollectionView.reloadData()
        }).disposed(by: disposeBag)
 
        maximumPriceField.rx.controlEvent(.editingDidBegin).subscribe(onNext: {[weak self]() in
            self?.selectIndex = -1
            self?.itemCollectionView.reloadData()
        }).disposed(by: disposeBag)
 
 
        minimumPriceField.rx.controlEvent(.editingDidEnd).subscribe(onNext: {[weak self]() in
            self?.checkSelect()
        }).disposed(by: disposeBag)
 
        maximumPriceField.rx.controlEvent(.editingDidEnd).subscribe(onNext: {[weak self]() in
            self?.checkSelect()
        }).disposed(by: disposeBag)
    }
 
    private func checkSelect(){
        let min = minimumPriceField.text
        let max = maximumPriceField.text
 
        switch (min,max) {
            case ("",""):selectIndex = 0;itemCollectionView.reloadData()
            case ("","40000"):selectIndex = 1;itemCollectionView.reloadData()
            case ("40000","60000"):selectIndex = 2;itemCollectionView.reloadData()
            case ("60000","80000"):selectIndex = 3;itemCollectionView.reloadData()
            case ("80000",""):selectIndex = 4;itemCollectionView.reloadData()
            default:break
        }
    }
 
    @discardableResult
    static func show(_ vc:YYViewController,offsetTop:CGFloat = 0,selectIndex:Int = 0,selectClouse:@escaping (Int,Double?,Double?)->Void,hiddenClouse:@escaping ()->Void)->CarPriceListView{
        let carBrandListView = CarPriceListView.ld_loadNibView()
        carBrandListView.hiddenClouse = hiddenClouse
        carBrandListView.completeClouse = selectClouse
        carBrandListView.selectIndex = selectIndex
 
        vc.view.addSubview(carBrandListView)
        carBrandListView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: offsetTop, left: 0, bottom: 0, right: 0))
        }
 
        UIView.animate(withDuration: 0.4) {
            carBrandListView.alpha = 1
            carBrandListView.layoutIfNeeded()
            carBrandListView.itemCollectionView.reloadData()
        }
 
        return carBrandListView
    }
 
 
    @IBAction func hiddenAction(_ sender: UIButton) {
        hidden()
    }
 
    @IBAction func resetAction(_ sender: UIButton) {
        selectIndex = 0
        minimumPriceField.text = ""
        maximumPriceField.text = ""
        maxPrice = nil
        minPrice = nil
        itemCollectionView.reloadData()
    }
 
    @IBAction func completeAction(_ sender: UIButton) {
 
        if !maximumPriceField.text!.isEmpty || !minimumPriceField.text!.isEmpty{
 
            if !minimumPriceField.text!.isEmpty{
                minPrice = Double(minimumPriceField.text!)!
            }
 
            if !maximumPriceField.text!.isEmpty{
                maxPrice = Double(maximumPriceField.text!)!
            }
 
            completeClouse?(selectIndex,minPrice,maxPrice)
            hidden()
            return
        }
 
        switch selectIndex {
            case 1:
                minPrice = nil
                maxPrice = 40000
            case 2:
                minPrice = 40000
                maxPrice = 60000
            case 3:
                minPrice = 60000
                maxPrice = 80000
            case 4:
                minPrice = 80000
                maxPrice = nil
            default:
                minPrice = nil
                maxPrice = nil
        }
 
        completeClouse?(selectIndex,minPrice,maxPrice)
        hidden()
    }
 
    func hidden(){
        UIView.animate(withDuration: 0.4) {
           self.alpha = 0
           self.layoutIfNeeded()
        } completion: { _ in
            self.hiddenClouse?()
            self.removeFromSuperview()
        }
    }
 
    private func calHei(){
        let h = ceil(Double(items.count) / 4.0) * cellH + floor(Double(items.count) / 4.0) * 15
        itemCollectionHeiCons.constant = h
    }
}
 
extension CarPriceListView:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectIndex = indexPath.row
 
        switch selectIndex {
            case 1:
                minimumPriceField.text = ""
                maximumPriceField.text = "40000"
            case 2:
                minimumPriceField.text = "40000"
                maximumPriceField.text = "60000"
            case 3:
                minimumPriceField.text = "60000"
                maximumPriceField.text = "80000"
            case 4:
                minimumPriceField.text = "80000"
                maximumPriceField.text = ""
            default:
                minimumPriceField.text = ""
                maximumPriceField.text = ""
                minPrice = nil
                maxPrice = nil
        }
 
        collectionView.reloadData()
    }
}
 
extension CarPriceListView:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_Common_SingleText_CCell", for: indexPath) as! Common_SingleText_CCell
        cell.titleL.text = items[indexPath.row]
        cell.titleL.cornerRadius = 2
        cell.titleL.maskToBounds = true
        cell.borderColor = UIColor.color(hexString: "#EFEFEF")
        if indexPath.row == selectIndex{
            cell.titleL.backgroundColor = UIColor.color(hexString: "#00BF30")
            cell.titleL.textColor = .white
            cell.borderWidth = 0
        }else{
            cell.titleL.backgroundColor = .white
            cell.titleL.textColor = UIColor.color(hexString: "#727272")
            cell.borderWidth = 0.5
        }
        return cell
    }
}
 
extension CarPriceListView:UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 15
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        CGSize(width: cellW, height: cellH)
    }
}