-
-
Notifications
You must be signed in to change notification settings - Fork 487
/
Copy pathDropBoxView.swift
138 lines (118 loc) · 3.98 KB
/
DropBoxView.swift
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
//
// DropBoxView.swift
// Bark
//
// Created by huangfeng on 2023/2/2.
// Copyright © 2023 Fin. All rights reserved.
//
import DropDown
import UIKit
import RxCocoa
import RxSwift
class DropBoxView: UIView {
let valueLabel: UILabel = {
let label = UILabel()
label.font = UIFont.preferredFont(ofSize: 14)
label.adjustsFontForContentSizeCategory = true
label.textColor = BKColor.grey.darken3
return label
}()
let dropIconView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "baseline_keyboard_arrow_down_black_24pt")?.withRenderingMode(.alwaysTemplate)
imageView.tintColor = BKColor.grey.lighten2
return imageView
}()
var isSelecting: Bool = true {
didSet {
UIView.animate(withDuration: 0.3) {
if self.isSelecting {
self.borderColor = BKColor.blue.darken5
self.shadowColor = BKColor.blue.darken5
self.layer.shadowOpacity = 0.3
}
else {
self.borderColor = BKColor.grey.lighten2
self.shadowColor = BKColor.grey.lighten2
self.layer.shadowOpacity = 0
}
}
}
}
var values: [String] {
didSet {
self.currentValue = values.first
}
}
var currentValue: String? {
didSet {
self.valueLabel.text = currentValue
self.currentValueChanged?(self.currentValue)
}
}
var currentValueChanged: ((String?) -> Void)?
init(values: [String]) {
self.values = values
super.init(frame: CGRect.zero)
self.backgroundColor = BKColor.white
self.borderColor = BKColor.grey.lighten2
self.borderWidthPreset = .border2
self.cornerRadiusPreset = .cornerRadius3
self.shadowColor = BKColor.grey.lighten2
self.layer.shadowOffset = CGSize(width: 0, height: 0)
self.layer.shadowRadius = 2
self.layer.shadowOpacity = 0
addSubview(valueLabel)
addSubview(dropIconView)
self.dropIconView.snp.makeConstraints { make in
make.centerY.equalToSuperview()
make.right.equalTo(-6)
}
self.valueLabel.snp.makeConstraints { make in
make.centerY.equalToSuperview()
make.left.equalTo(16)
}
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap)))
defer {
self.currentValue = self.values.first
}
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func tap() {
let dropDown = DropDown(anchorView: self)
dropDown.cellNib = UINib(nibName: "BKDropDownCell", bundle: Bundle(for: BKDropDownCell.self))
dropDown.cellHeight = 50
dropDown.cornerRadius = 10
dropDown.clipsToBounds = true
dropDown.bottomOffset = CGPoint(x: 0, y: 50)
dropDown.dataSource = self.values
dropDown.selectionAction = { [weak self] _, str in
self?.currentValue = str
self?.isSelecting = false
}
dropDown.cancelAction = { [weak self] in
self?.isSelecting = false
}
self.isSelecting = true
dropDown.show()
}
}
extension Reactive where Base: DropBoxView {
var currentValueChanged: ControlEvent<String?> {
let source = Observable<String?>.create { [weak control = self.base] observer -> Disposable in
MainScheduler.ensureExecutingOnScheduler()
guard let control = control else {
observer.onCompleted()
return Disposables.create()
}
control.currentValueChanged = { value in
observer.onNext(value)
}
return Disposables.create()
}
return ControlEvent(events: source)
}
}