forked from aguynamedloren/ios-picker-dialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PickerDialog.swift
268 lines (215 loc) · 11 KB
/
PickerDialog.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
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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// PickerDialog.swift
//
// Created by Loren Burton on 02/08/2016.
//
import Foundation
import UIKit
import QuartzCore
class PickerDialog: UIView, UIPickerViewDataSource, UIPickerViewDelegate {
typealias PickerCallback = (value: String) -> Void
/* Constants */
private let kPickerDialogDefaultButtonHeight: CGFloat = 50
private let kPickerDialogDefaultButtonSpacerHeight: CGFloat = 1
private let kPickerDialogCornerRadius: CGFloat = 7
private let kPickerDialogDoneButtonTag: Int = 1
/* Views */
private var dialogView: UIView!
private var titleLabel: UILabel!
private var picker: UIPickerView!
private var cancelButton: UIButton!
private var doneButton: UIButton!
/* Variables */
private var pickerData = [[String: String]]()
private var selectedPickerValue: String?
private var callback: PickerCallback?
/* Overrides */
init() {
super.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
setupView()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupView() {
self.dialogView = createContainerView()
self.dialogView!.layer.shouldRasterize = true
self.dialogView!.layer.rasterizationScale = UIScreen.mainScreen().scale
self.layer.shouldRasterize = true
self.layer.rasterizationScale = UIScreen.mainScreen().scale
self.dialogView!.layer.opacity = 0.5
self.dialogView!.layer.transform = CATransform3DMakeScale(1.3, 1.3, 1)
self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0)
picker.delegate = self
self.addSubview(self.dialogView!)
}
/* Handle device orientation changes */
func deviceOrientationDidChange(notification: NSNotification) {
close() // For now just close it
}
/* Required UIPickerView functions */
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]["display"]
}
/* Helper to find row of selected value */
func findIndexForValue(value: String, array: [[String: String]]) -> Int? {
for (index, dictionary) in array.enumerate() {
if dictionary["value"] == value {
return index
}
}
return nil
}
/* Create the dialog view, and animate opening the dialog */
func show(title: String, doneButtonTitle: String = "Done", cancelButtonTitle: String = "Cancel", options: [[String: String]], selected: String? = nil, callback: PickerCallback) {
self.titleLabel.text = title
self.pickerData = options
self.doneButton.setTitle(doneButtonTitle, forState: .Normal)
self.cancelButton.setTitle(cancelButtonTitle, forState: .Normal)
self.callback = callback
if selected != nil {
self.selectedPickerValue = selected
let selectedIndex = findIndexForValue(selected!, array: options) ?? 0
self.picker.selectRow(selectedIndex, inComponent: 0, animated: false)
}
/* */
UIApplication.sharedApplication().windows.first!.addSubview(self)
UIApplication.sharedApplication().windows.first!.endEditing(true)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "deviceOrientationDidChange:", name: UIDeviceOrientationDidChangeNotification, object: nil)
/* Anim */
UIView.animateWithDuration(
0.2,
delay: 0,
options: UIViewAnimationOptions.CurveEaseInOut,
animations: { () -> Void in
self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4)
self.dialogView!.layer.opacity = 1
self.dialogView!.layer.transform = CATransform3DMakeScale(1, 1, 1)
},
completion: nil
)
}
/* Dialog close animation then cleaning and removing the view from the parent */
private func close() {
NSNotificationCenter.defaultCenter().removeObserver(self)
let currentTransform = self.dialogView.layer.transform
let startRotation = (self.valueForKeyPath("layer.transform.rotation.z") as? NSNumber) as? Double ?? 0.0
let rotation = CATransform3DMakeRotation((CGFloat)(-startRotation + M_PI * 270 / 180), 0, 0, 0)
self.dialogView.layer.transform = CATransform3DConcat(rotation, CATransform3DMakeScale(1, 1, 1))
self.dialogView.layer.opacity = 1
UIView.animateWithDuration(
0.2,
delay: 0,
options: UIViewAnimationOptions.TransitionNone,
animations: { () -> Void in
self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0)
self.dialogView.layer.transform = CATransform3DConcat(currentTransform, CATransform3DMakeScale(0.6, 0.6, 1))
self.dialogView.layer.opacity = 0
}) { (finished: Bool) -> Void in
for v in self.subviews {
v.removeFromSuperview()
}
self.removeFromSuperview()
}
}
/* Creates the container view here: create the dialog, then add the custom content and buttons */
private func createContainerView() -> UIView {
let screenSize = countScreenSize()
let dialogSize = CGSizeMake(
300,
230
+ kPickerDialogDefaultButtonHeight
+ kPickerDialogDefaultButtonSpacerHeight)
// For the black background
self.frame = CGRectMake(0, 0, screenSize.width, screenSize.height)
// This is the dialog's container; we attach the custom content and the buttons to this one
let dialogContainer = UIView(frame: CGRectMake((screenSize.width - dialogSize.width) / 2, (screenSize.height - dialogSize.height) / 2, dialogSize.width, dialogSize.height))
// First, we style the dialog to match the iOS8 UIAlertView >>>
let gradient: CAGradientLayer = CAGradientLayer(layer: self.layer)
gradient.frame = dialogContainer.bounds
gradient.colors = [UIColor(red: 218/255, green: 218/255, blue: 218/255, alpha: 1).CGColor,
UIColor(red: 233/255, green: 233/255, blue: 233/255, alpha: 1).CGColor,
UIColor(red: 218/255, green: 218/255, blue: 218/255, alpha: 1).CGColor]
let cornerRadius = kPickerDialogCornerRadius
gradient.cornerRadius = cornerRadius
dialogContainer.layer.insertSublayer(gradient, atIndex: 0)
dialogContainer.layer.cornerRadius = cornerRadius
dialogContainer.layer.borderColor = UIColor(red: 198/255, green: 198/255, blue: 198/255, alpha: 1).CGColor
dialogContainer.layer.borderWidth = 1
dialogContainer.layer.shadowRadius = cornerRadius + 5
dialogContainer.layer.shadowOpacity = 0.1
dialogContainer.layer.shadowOffset = CGSizeMake(0 - (cornerRadius + 5) / 2, 0 - (cornerRadius + 5) / 2)
dialogContainer.layer.shadowColor = UIColor.blackColor().CGColor
dialogContainer.layer.shadowPath = UIBezierPath(roundedRect: dialogContainer.bounds, cornerRadius: dialogContainer.layer.cornerRadius).CGPath
// There is a line above the button
let lineView = UIView(frame: CGRectMake(0, dialogContainer.bounds.size.height - kPickerDialogDefaultButtonHeight - kPickerDialogDefaultButtonSpacerHeight, dialogContainer.bounds.size.width, kPickerDialogDefaultButtonSpacerHeight))
lineView.backgroundColor = UIColor(red: 198/255, green: 198/255, blue: 198/255, alpha: 1)
dialogContainer.addSubview(lineView)
// ˆˆˆ
//Title
self.titleLabel = UILabel(frame: CGRectMake(10, 10, 280, 30))
self.titleLabel.textAlignment = NSTextAlignment.Center
self.titleLabel.textColor = UIColor(hex: 0x333333)
self.titleLabel.font = UIFont(name: "AvenirNext-Medium", size: 16)
dialogContainer.addSubview(self.titleLabel)
self.picker = UIPickerView(frame: CGRectMake(0, 30, 0, 0))
self.picker.setValue(UIColor(hex: 0x333333), forKeyPath: "textColor")
self.picker.autoresizingMask = UIViewAutoresizing.FlexibleRightMargin
self.picker.frame.size.width = 300
dialogContainer.addSubview(self.picker)
// Add the buttons
addButtonsToView(dialogContainer)
return dialogContainer
}
/* Add buttons to container */
private func addButtonsToView(container: UIView) {
let buttonWidth = container.bounds.size.width / 2
self.cancelButton = UIButton(type: UIButtonType.Custom) as UIButton
self.cancelButton.frame = CGRectMake(
0,
container.bounds.size.height - kPickerDialogDefaultButtonHeight,
buttonWidth,
kPickerDialogDefaultButtonHeight
)
self.cancelButton.setTitleColor(UIColor(hex: 0x555555), forState: UIControlState.Normal)
self.cancelButton.setTitleColor(UIColor(hex: 0x555555), forState: UIControlState.Highlighted)
self.cancelButton.titleLabel!.font = UIFont(name: "AvenirNext-Medium", size: 15)
self.cancelButton.layer.cornerRadius = kPickerDialogCornerRadius
self.cancelButton.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
container.addSubview(self.cancelButton)
self.doneButton = UIButton(type: UIButtonType.Custom) as UIButton
self.doneButton.frame = CGRectMake(
buttonWidth,
container.bounds.size.height - kPickerDialogDefaultButtonHeight,
buttonWidth,
kPickerDialogDefaultButtonHeight
)
self.doneButton.tag = kPickerDialogDoneButtonTag
self.doneButton.setTitleColor(UIColor(hex: 0x555555), forState: UIControlState.Normal)
self.doneButton.setTitleColor(UIColor(hex: 0x555555), forState: UIControlState.Highlighted)
self.doneButton.titleLabel!.font = UIFont(name: "AvenirNext-Medium", size: 15)
self.doneButton.layer.cornerRadius = kPickerDialogCornerRadius
self.doneButton.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
container.addSubview(self.doneButton)
}
func buttonTapped(sender: UIButton!) {
if sender.tag == kPickerDialogDoneButtonTag {
let selectedIndex = self.picker.selectedRowInComponent(0)
let selectedValue = self.pickerData[selectedIndex]["value"]
self.callback?(value: selectedValue!)
}
close()
}
/* Helper function: count and return the screen's size */
func countScreenSize() -> CGSize {
let screenWidth = UIScreen.mainScreen().applicationFrame.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
return CGSizeMake(screenWidth, screenHeight)
}
}