CNLiveLoginPickerView.swift
6.01 KB
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
//
// CNLiveLoginPickerView.swift
// metaCode
//
// Created by open on 2023/6/5.
//
import Foundation
typealias CloseClickResultBlock = (_ text: String) ->Void
class CNLiveLoginPickerView : UIView, UIPickerViewDelegate,UIPickerViewDataSource {
lazy var tapGesture = UITapGestureRecognizer()
lazy var grayMaskView: UIView = {
let grayMaskView = UIView()
tapGesture.addTarget(self, action: #selector(setupCloseButtonAction))
grayMaskView.backgroundColor = UIColor.black.withAlphaComponent(0.2)
grayMaskView.addGestureRecognizer(tapGesture)
return grayMaskView
}()
lazy var backView: UIView = {
let backView = UIView()
backView.backgroundColor = UIColor.white
backView.layer.cornerRadius = 12
return backView
}()
lazy var pickerView : UIPickerView = {
let pickerView = UIPickerView.init()
pickerView.backgroundColor = .white
pickerView.showsSelectionIndicator = true
pickerView.delegate = self
pickerView.dataSource = self
return pickerView
}()
lazy var lineLable : UILabel = {
let lineLable = UILabel.init()
lineLable.backgroundColor = HexColor("#E6E6E6")
return lineLable
}()
lazy var doneButton : UIButton = {
let doneButton = UIButton(type: .custom)
doneButton.setTitle("确定", for: .normal)
doneButton.setTitleColor(HexColor("#0066FE"), for: .normal)
doneButton.titleLabel?.font = Font_16
doneButton.addTarget(self, action: #selector(setupDoneButtonAction), for: .touchUpInside)
return doneButton
}()
lazy var closeButton : UIButton = {
let closeButton = UIButton(type: .custom)
closeButton.setImage(UIImage(named: "login_alert_country_close"), for: .normal)
closeButton.backgroundColor = .clear
closeButton.addTarget(self, action: #selector(setupCloseButtonAction), for: .touchUpInside)
return closeButton
}()
lazy var country_array : NSArray = {
let array = NSArray(contentsOfFile: Bundle.main.path(forResource: "countries", ofType: "plist") ?? "")!
return array
}()
var resutBlock : CloseClickResultBlock?
var result_string : String = ""
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(grayMaskView)
addSubview(backView)
backView.addSubview(lineLable)
backView.addSubview(pickerView)
backView.addSubview(closeButton)
backView.addSubview(doneButton)
setConstraints()
}
func setConstraints() {
grayMaskView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
backView.snp.makeConstraints { make in
make.top.equalTo(self.snp.bottom).offset(0)
make.left.right.equalToSuperview()
}
var safeBottomHeight: CGFloat = 0
if #available(iOS 11.0, *) {
safeBottomHeight = UIApplication.shared.windows.last?.safeAreaInsets.bottom ?? 0
} else {
// Fallback on earlier versions
}
pickerView.snp.makeConstraints { make in
make.bottom.equalTo(-safeBottomHeight-20)
make.left.right.equalToSuperview()
make.top.equalTo(50)
}
doneButton.snp.makeConstraints { make in
make.right.equalToSuperview()
make.top.equalTo(10)
make.height.equalTo(30)
make.width.equalTo(60)
}
closeButton.snp.makeConstraints { make in
make.centerY.equalTo(doneButton)
make.left.equalTo(8)
make.size.equalTo(30)
}
lineLable.snp.makeConstraints { make in
make.left.right.equalToSuperview()
make.height.equalTo(1)
make.top.equalTo(pickerView.snp.top).offset(-1)
}
}
static public func showSelectView(resultBlock: @escaping CloseClickResultBlock){
let pickerView = CNLiveLoginPickerView()
pickerView.resutBlock = resultBlock
let window = UIApplication.shared.windows.last
window?.addSubview(pickerView)
pickerView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
window?.layoutIfNeeded()
pickerView.alpha = 0
UIView.animate(withDuration: 0.1) {
pickerView.alpha = 1
} completion: { _ in
UIView.animate(withDuration: 0.1) {
pickerView.backView.snp.updateConstraints { make in
make.top.equalTo(pickerView.snp.bottom).offset(-pickerView.backView.frame.height)
}
pickerView.layoutIfNeeded()
}
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
}
}
extension CNLiveLoginPickerView {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return country_array.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let param = country_array[row] as! NSDictionary
let name = param["name"] as? String
let code = param["code"] as? String
return "\(name ?? "")"+"(+\(code ?? ""))"
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let param = country_array[row] as! NSDictionary
result_string = param["code"] as! String
}
}
extension CNLiveLoginPickerView {
@objc func setupDoneButtonAction(){
if let resutBlock = resutBlock {
resutBlock(result_string)
}
disMissView()
}
@objc func setupCloseButtonAction() -> Void{
disMissView()
}
func disMissView() {
resutBlock = nil
removeFromSuperview()
}
}