f2cf74c7
yangbin
1.0.20(4)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//
// DatePickerView.swift
// Twear
//
// Created by yangbin on 2021/12/24.
//
import UIKit
class ZCPickerView: UIView, UIPickerViewDelegate, UIPickerViewDataSource {
typealias clickAlertClosure = (_ value: Int) -> Void
var clickClosure: clickAlertClosure!
private let bgView = UIView() //白色框
private lazy var titleLabel: UILabel = {
let label = UILabel()
|
5bd43cbb
daifengyi
feat:picker view UI
|
19
20
|
label.font = RegularFont(20)
label.textColor = UIColor .rgbColorFromHex(0x333333)
|
f2cf74c7
yangbin
1.0.20(4)
|
21
22
23
24
25
26
27
|
label.textAlignment = .center
return label
}()
private lazy var sureBtn: UIButton = {
let button = UIButton()
button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside)
|
5bd43cbb
daifengyi
feat:picker view UI
|
28
29
30
|
button.layer.cornerRadius = 22
button.layer.masksToBounds = true
button.backgroundColor = UIColor .rgbColorFromHex(0x24C789)
|
f2cf74c7
yangbin
1.0.20(4)
|
31
|
button.setTitle(LocString("确定"), for: .normal)
|
5bd43cbb
daifengyi
feat:picker view UI
|
32
|
button.titleLabel?.font = BoldFont(17)
|
f2cf74c7
yangbin
1.0.20(4)
|
33
34
35
36
37
38
39
|
button.tag = 1
return button
}()
private lazy var cancelBtn: UIButton = {
let button = UIButton()
button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside)
|
5bd43cbb
daifengyi
feat:picker view UI
|
40
|
button.setImage(UIImage(named: "close"), for: .normal)
|
f2cf74c7
yangbin
1.0.20(4)
|
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
button.tag = 2
return button
}()
var selectedRow: Int = 0 {
didSet {
pickerView.selectRow(self.selectedRow, inComponent: 0, animated: false)
pickerView.reloadAllComponents()
}
}
lazy var pickerView: UIPickerView = {
let picker = UIPickerView()
picker.delegate = self
// picker.locale = .current
// picker.addTarget(self, action: #selector(dateChanged(datePicker:)), for: .valueChanged)
return picker
}()
private var suffix: String?
|
5bd43cbb
daifengyi
feat:picker view UI
|
61
|
private var titleColor: UIColor = UIColor.rgbColorFromHex(0x333333)
|
f2cf74c7
yangbin
1.0.20(4)
|
62
63
64
65
66
67
68
|
private var values: [Int] = []
// {
// didSet {
// pickerView.reloadAllComponents()
// }
// }
|
ebb6ae98
daifengyi
feat:data page na...
|
69
|
init(title: String, values: [Int], suffix: String? = nil, sureText: String = LocString("确定")) {
|
f2cf74c7
yangbin
1.0.20(4)
|
70
|
super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
|
ebb6ae98
daifengyi
feat:data page na...
|
71
|
createView(title: title, values: values, suffix: suffix, sureText: sureText)
|
f2cf74c7
yangbin
1.0.20(4)
|
72
73
74
|
}
|
ebb6ae98
daifengyi
feat:data page na...
|
75
|
func createView(title: String, values: [Int], suffix: String? = nil, sureText: String) {
|
f2cf74c7
yangbin
1.0.20(4)
|
76
77
78
|
self.backgroundColor = UIColor.black.withAlphaComponent(0.1)
bgView.backgroundColor = .white
|
f2cf74c7
yangbin
1.0.20(4)
|
79
80
81
82
|
self.suffix = suffix
self.values = values
titleLabel.text = title
sureBtn.setTitle(sureText, for: .normal)
|
f2cf74c7
yangbin
1.0.20(4)
|
83
84
85
|
// pickerView.reloadAllComponents()
|
f2cf74c7
yangbin
1.0.20(4)
|
86
87
88
89
|
addSubview(bgView)
bgView.addSubview(titleLabel)
bgView.addSubview(sureBtn)
bgView.addSubview(cancelBtn)
|
f2cf74c7
yangbin
1.0.20(4)
|
90
91
92
93
|
bgView.addSubview(pickerView)
bgView.snp.makeConstraints { (make) in
make.left.right.bottom.equalToSuperview()
|
5bd43cbb
daifengyi
feat:picker view UI
|
94
|
make.height.equalTo(340)
|
f2cf74c7
yangbin
1.0.20(4)
|
95
96
|
}
cancelBtn.snp.makeConstraints { make in
|
5bd43cbb
daifengyi
feat:picker view UI
|
97
98
99
|
make.height.equalTo(78)
make.width.equalTo(58)
make.right.top.equalToSuperview()
|
f2cf74c7
yangbin
1.0.20(4)
|
100
101
102
|
}
sureBtn.snp.makeConstraints { make in
|
5bd43cbb
daifengyi
feat:picker view UI
|
103
104
105
106
|
make.bottom.equalToSuperview().offset(-50)
make.left.equalToSuperview().offset(60)
make.right.equalToSuperview().offset(-60)
make.height.equalTo(44)
|
f2cf74c7
yangbin
1.0.20(4)
|
107
|
}
|
5bd43cbb
daifengyi
feat:picker view UI
|
108
|
|
f2cf74c7
yangbin
1.0.20(4)
|
109
110
111
112
|
titleLabel.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.centerY.equalTo(cancelBtn)
}
|
f2cf74c7
yangbin
1.0.20(4)
|
113
|
pickerView.snp.makeConstraints { make in
|
5bd43cbb
daifengyi
feat:picker view UI
|
114
115
116
|
make.top.equalTo(cancelBtn.snp_bottom)
make.left.right.equalToSuperview()
make.bottom.equalTo(sureBtn.snp_top)
|
f2cf74c7
yangbin
1.0.20(4)
|
117
118
119
120
|
}
bgView.layoutIfNeeded()
|
5bd43cbb
daifengyi
feat:picker view UI
|
121
|
bgView.setCorners(corners: [.topLeft, .topRight], radio: 12)
|
f2cf74c7
yangbin
1.0.20(4)
|
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return values.count
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if suffix == ":00" {
return "\(String(format:"%02d",values[row])):00"
}
return "\(values[row])"
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var pickerLabel = view as? UILabel
if pickerLabel == nil {
pickerLabel = UILabel()
|
5bd43cbb
daifengyi
feat:picker view UI
|
145
|
pickerLabel?.font = RegularFont(16)
|
f2cf74c7
yangbin
1.0.20(4)
|
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
|
pickerLabel?.textAlignment = .center
pickerLabel?.textColor = UIColor.lightGray
}
if row == selectedRow {
pickerLabel?.attributedText = self.pickerView(pickerView, attributedTitleForRow: selectedRow, forComponent: component)
} else {
pickerLabel?.text = self.pickerView(pickerView, titleForRow: row, forComponent: component)
}
return pickerLabel!
}
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 30
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let str = "\(values[row])"
var string = NSMutableAttributedString(string: "\(values[row])")
if suffix != nil {
if suffix == ":00" {
string = NSMutableAttributedString(string: "\(String(format:"%02d",values[row])):00")
string.addAttributes([.foregroundColor: titleColor, .font: RegularFont(23)], range: NSMakeRange(0, string.length))
} else {
string = NSMutableAttributedString(string: " \(values[row]) \(suffix!)")
|
5bd43cbb
daifengyi
feat:picker view UI
|
171
172
173
|
string.addAttributes([.foregroundColor: titleColor, .font: BoldFont(16)], range: NSMakeRange(0, string.length-1))
string.addAttributes([.foregroundColor: titleColor, .font: BoldFont(20)], range: NSMakeRange(0, str.length+1))
string.addAttributes([.foregroundColor: titleColor, .font: BoldFont(16)], range: NSMakeRange(str.length+1, suffix!.length+1))
|
f2cf74c7
yangbin
1.0.20(4)
|
174
175
|
}
} else {
|
5bd43cbb
daifengyi
feat:picker view UI
|
176
|
string.addAttributes([.foregroundColor: titleColor, .font: BoldFont(20)], range: NSMakeRange(0, str.length))
|
f2cf74c7
yangbin
1.0.20(4)
|
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
|
}
return string
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedRow = row
pickerView.reloadAllComponents()
}
@objc func clickBtnAction(_ sender: UIButton) {
if clickClosure != nil && sender.tag == 1 {
clickClosure!(values[selectedRow])
}
dismiss()
}
func show() {
let wind = KeyWindow
self.alpha = 0
wind.addSubview(self)
UIView.animate(withDuration: 0.25, animations: { () -> Void in
self.alpha = 1
})
}
@objc func dismiss() {
UIView.animate(withDuration: 0.25, animations: { () -> Void in
self.alpha = 0
}, completion: { (finish) -> Void in
if finish {
self.removeFromSuperview()
}
})
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
}
|