-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPopUpPickerView.swift
135 lines (113 loc) · 4.2 KB
/
PopUpPickerView.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
import UIKit
#if !RX_NO_MODULE
import RxSwift
import RxCocoa
#endif
public enum PopUpPickerViewStyle
{
case Default
case WithSegementedControl
}
class PopUpPickerView: PopUpPickerViewBase {
var pickerView: UIPickerView!
lazy var itemSelected: Driver<[Int]> = {
return self.doneButtonItem.rx_tap.asDriver()
.map { _ in
self.hidePicker()
self.selectedRows = nil
return self.getSelectedRows()
}
.startWith(self.selectedRows ?? [])
}()
var segmentedControl: UISegmentedControl?
private var initSegementedControl: Bool = false
private let segementedControlHeight: CGFloat = 29
private let segementedControlSuperViewHeight: CGFloat = 29 + 16
var delegate: PopUpPickerViewDelegate? {
didSet {
pickerView.delegate = delegate
}
}
internal var selectedRows: [Int]?
// MARK: Initializer
override init() {
super.init()
initFunc()
}
override init(frame: CGRect) {
super.init(frame: frame)
initFunc()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initFunc()
}
convenience init(rows: [Int], style: PopUpPickerViewStyle = .Default) {
self.init()
selectedRows = rows
if style == .WithSegementedControl {
let screenSize = UIScreen.mainScreen().bounds.size
let frame = pickerView.frame
pickerView.frame = CGRect(x: frame.origin.x, y: frame.origin.y + segementedControlSuperViewHeight, width: frame.size.width, height: frame.size.height)
// MARK:Add board view
let v = UIView(frame: CGRect(x: 0, y: 44, width: screenSize.width, height: segementedControlSuperViewHeight))
v.backgroundColor = UIColor.whiteColor()
self.addSubview(v)
let edge = UIEdgeInsets(top: 16, left: 16, bottom: 0, right: 16)
segmentedControl = UISegmentedControl(frame: CGRect(x: edge.left, y:edge.top, width: screenSize.width - (edge.left + edge.right), height: segementedControlHeight) )
v.addSubview(segmentedControl!)
}
}
private func initFunc() {
let screenSize = UIScreen.mainScreen().bounds.size
pickerView = UIPickerView()
pickerView.showsSelectionIndicator = true
pickerView.backgroundColor = UIColor.whiteColor()
pickerView.bounds = CGRectMake(0, 0, screenSize.width, 216)
pickerView.frame = CGRectMake(0, 44, screenSize.width, 216)
self.addSubview(pickerView)
}
// MARK: Actions
override func showPicker() {
if selectedRows == nil {
selectedRows = getSelectedRows()
}
if let selectedRows = selectedRows {
for (component, row) in selectedRows.enumerate() {
pickerView.selectRow(row, inComponent: component, animated: false)
}
}
let screenSize = UIScreen.mainScreen().bounds.size
let segmentedControlHeight: CGFloat = (segmentedControl != nil) ? self.segementedControlSuperViewHeight : 0
UIView.animateWithDuration(0.2) {
self.frame = CGRectMake(0, self.parentViewHeight() - (260.0 + segmentedControlHeight), screenSize.width, 260.0 + segmentedControlHeight)
}
}
override func cancelPicker() {
hidePicker()
restoreSelectedRows()
selectedRows = nil
}
override func endPicker() {
hidePicker()
delegate?.pickerView?(pickerView, didSelect: getSelectedRows())
selectedRows = nil
}
internal func getSelectedRows() -> [Int] {
var selectedRows = [Int]()
for i in 0..<pickerView.numberOfComponents {
selectedRows.append(pickerView.selectedRowInComponent(i))
}
return selectedRows
}
private func restoreSelectedRows() {
guard let selectedRows = selectedRows else { return }
for i in 0..<selectedRows.count {
pickerView.selectRow(selectedRows[i], inComponent: i, animated: true)
}
}
}
@objc
protocol PopUpPickerViewDelegate: UIPickerViewDelegate {
optional func pickerView(pickerView: UIPickerView, didSelect numbers: [Int])
}