无故事王国
2024-03-12 fd05d3dfc61ad3482cfd577279cff72f74c68cf6
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
//
//  CommonYearsPickerView.swift
//  WanPai
//
//  Created by 无故事王国 on 2024/3/5.
//
 
import UIKit
import JQTools
import QMUIKit
import RxSwift
import RxCocoa
 
class CommonYearsPickerView: UIView,JQNibView{
 
                @IBOutlet weak var view_container: UIView!
                @IBOutlet weak var cons_bottom: NSLayoutConstraint!
                @IBOutlet weak var pickerView: UIPickerView!
                
                private var clickClouse:((Int)->Void)!
                private var disposeBag = DisposeBag()
                private var years = [Int]()
 
                override func awakeFromNib() {
                                super.awakeFromNib()
                                years = (Date().jq_nowYear()-5...Date().jq_nowYear()).map{$0}.sorted(by: {$0 > $1})
                                years.insert(0, at: 0)
                                cons_bottom.constant = -(JQ_ScreenW * 1.1)
                                pickerView.delegate = self
                                pickerView.dataSource = self
                                alpha = 0
                                layoutIfNeeded()
                                setRx()
                }
 
                static func show(clickClouse:@escaping (Int)->Void){
                                let studentChooseView = CommonYearsPickerView.jq_loadNibView()
                                studentChooseView.frame = sceneDelegate?.window?.frame ?? .zero
                                studentChooseView.clickClouse = clickClouse
                                sceneDelegate?.window?.addSubview(studentChooseView)
                                studentChooseView.cons_bottom.constant = 0
 
                                UIView.animate(withDuration: 0.4) {
                                                studentChooseView.alpha = 1
                                                studentChooseView.layoutIfNeeded()
                                }
                }
 
                private func setRx(){
 
                }
 
                @IBAction func closeAction(_ sender: UIButton) {
                                closeAction()
                }
 
                override func layoutSubviews() {
                                super.layoutSubviews()
                                DispatchQueue.main.asyncAfter(wallDeadline: .now()+0.1) {
                                                self.view_container.jq_addCorners(corner: [.topLeft,.topRight], radius: 20)
                                }
                }
 
                private func closeAction(){
                                self.cons_bottom.constant = -(JQ_ScreenW * 1.1)
                                UIView.animate(withDuration: 0.4) {
                                                self.alpha = 0
                                                self.layoutIfNeeded()
                                } completion: { _ in
                                                self.removeFromSuperview()
                                }
                }
 
                @IBAction func completeAction(_ sender: UIButton) {
                                let index = pickerView.selectedRow(inComponent: 0)
                                clickClouse!(years[index])
                                closeAction()
                }
}
 
extension CommonYearsPickerView:UIPickerViewDelegate{
 
                func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
                                let label = UILabel()
 
                                if years[row] == 0{
                                                label.text = "全部"
                                }else{
                                                label.text = "\(years[row])年"
                                }
                                label.textColor = UIColor(hexString: "3C3C3C")
                                label.textAlignment = .center
                                label.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
                                label.bounds = CGRect(origin: .zero, size: CGSize(width: 100, height: 45))
                                return label
                }
 
                func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
                                return 50
                }
}
 
extension CommonYearsPickerView:UIPickerViewDataSource{
                func numberOfComponents(in pickerView: UIPickerView) -> Int {
                                return 1
                }
                
                func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
                                return years.count
                }
}