//
|
// 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)
|
}
|
}
|