1
+ //
2
+ // DatePicker.swift
3
+
4
+ import Foundation
1
5
import UIKit
2
6
import QuartzCore
3
7
8
+
4
9
class DatePickerDialog : UIView {
5
10
6
11
/* Consts */
@@ -25,15 +30,16 @@ class DatePickerDialog: UIView {
25
30
private var datePickerMode : UIDatePickerMode !
26
31
private var callback : ( ( date: NSDate ) -> Void ) !
27
32
33
+
28
34
/* Overrides */
29
- override init ( ) {
35
+ init ( ) {
30
36
super. init ( frame: CGRectMake ( 0 , 0 , UIScreen . mainScreen ( ) . bounds. size. width, UIScreen . mainScreen ( ) . bounds. size. height) )
31
37
32
38
NSNotificationCenter . defaultCenter ( ) . addObserver ( self , selector: " deviceOrientationDidChange: " , name: UIDeviceOrientationDidChangeNotification, object: nil )
33
39
}
34
-
40
+
35
41
required init ( coder aDecoder: NSCoder ) {
36
- super . init ( coder: aDecoder )
42
+ fatalError ( " init(coder:) has not been implemented " )
37
43
}
38
44
39
45
/* Handle device orientation changes */
@@ -42,11 +48,11 @@ class DatePickerDialog: UIView {
42
48
}
43
49
44
50
/* Create the dialog view, and animate opening the dialog */
45
- func show( # title: String, datePickerMode: UIDatePickerMode = . DateAndTime, callback: ( ( date: NSDate ) -> Void ) ) {
46
- show ( title: title , doneButtonTitle: " Done " , cancelButtonTitle: " Cancel " , datePickerMode: datePickerMode, callback: callback)
51
+ func show( title: String , datePickerMode: UIDatePickerMode = . DateAndTime, callback: ( ( date: NSDate ) -> Void ) ) {
52
+ show ( title, doneButtonTitle: " Done " , cancelButtonTitle: " Cancel " , datePickerMode: datePickerMode, callback: callback)
47
53
}
48
54
49
- func show( # title: String, do neButtonTitle: String, cancelButtonTitle: String, defaultDate: NSDate = NSDate( ) , datePickerMode: UIDatePickerMode = . DateAndTime, callback: ( ( date: NSDate ) -> Void ) ) {
55
+ func show( title: String , doneButtonTitle: String , cancelButtonTitle: String , defaultDate: NSDate = NSDate ( ) , datePickerMode: UIDatePickerMode = . DateAndTime, callback: ( ( date: NSDate ) -> Void ) ) {
50
56
self . title = title
51
57
self . doneButtonTitle = doneButtonTitle
52
58
self . cancelButtonTitle = cancelButtonTitle
@@ -71,27 +77,27 @@ class DatePickerDialog: UIView {
71
77
72
78
/* Attached to the top most window (make sure we are using the right orientation) */
73
79
let interfaceOrientation = UIApplication . sharedApplication ( ) . statusBarOrientation
74
-
80
+
75
81
switch ( interfaceOrientation) {
76
- case UIInterfaceOrientation . LandscapeLeft:
77
- let t : Double = M_PI * 270 / 180
78
- self . transform = CGAffineTransformMakeRotation ( CGFloat ( t) )
79
- break
82
+ case UIInterfaceOrientation . LandscapeLeft:
83
+ let t : Double = M_PI * 270 / 180
84
+ self . transform = CGAffineTransformMakeRotation ( CGFloat ( t) )
85
+ break
80
86
81
- case UIInterfaceOrientation . LandscapeRight:
82
- let t : Double = M_PI * 90 / 180
83
- self . transform = CGAffineTransformMakeRotation ( CGFloat ( t) )
84
- break
85
-
86
- case UIInterfaceOrientation . PortraitUpsideDown:
87
- let t : Double = M_PI * 180 / 180
88
- self . transform = CGAffineTransformMakeRotation ( CGFloat ( t) )
89
- break
90
-
91
- default :
92
- break
93
- }
87
+ case UIInterfaceOrientation . LandscapeRight:
88
+ let t : Double = M_PI * 90 / 180
89
+ self . transform = CGAffineTransformMakeRotation ( CGFloat ( t) )
90
+ break
91
+
92
+ case UIInterfaceOrientation . PortraitUpsideDown:
93
+ let t : Double = M_PI * 180 / 180
94
+ self . transform = CGAffineTransformMakeRotation ( CGFloat ( t) )
95
+ break
94
96
97
+ default :
98
+ break
99
+ }
100
+
95
101
self . frame = CGRectMake ( 0 , 0 , self . frame. width, self . frame. size. height)
96
102
UIApplication . sharedApplication ( ) . windows. first!. addSubview ( self )
97
103
@@ -113,7 +119,7 @@ class DatePickerDialog: UIView {
113
119
private func close( ) {
114
120
let currentTransform = self . dialogView. layer. transform
115
121
116
- let startRotation = ( self . valueForKeyPath ( " layer.transform.rotation.z " ) as? NSNumber ) ? as? Double ?? 0.0
122
+ let startRotation = ( self . valueForKeyPath ( " layer.transform.rotation.z " ) as? NSNumber ) as? Double ?? 0.0
117
123
let rotation = CATransform3DMakeRotation ( ( CGFloat) ( - startRotation + M_PI * 270 / 180 ) , 0 , 0 , 0 )
118
124
119
125
self . dialogView. layer. transform = CATransform3DConcat ( rotation, CATransform3DMakeScale ( 1 , 1 , 1 ) )
@@ -133,7 +139,7 @@ class DatePickerDialog: UIView {
133
139
}
134
140
135
141
self . removeFromSuperview ( )
136
- }
142
+ }
137
143
}
138
144
139
145
/* Creates the container view here: create the dialog, then add the custom content and buttons */
@@ -142,8 +148,8 @@ class DatePickerDialog: UIView {
142
148
let dialogSize = CGSizeMake (
143
149
300 ,
144
150
230
145
- + kDatePickerDialogDefaultButtonHeight
146
- + kDatePickerDialogDefaultButtonSpacerHeight)
151
+ + kDatePickerDialogDefaultButtonHeight
152
+ + kDatePickerDialogDefaultButtonSpacerHeight)
147
153
148
154
// For the black background
149
155
self . frame = CGRectMake ( 0 , 0 , screenSize. width, screenSize. height)
@@ -155,8 +161,8 @@ class DatePickerDialog: UIView {
155
161
let gradient : CAGradientLayer = CAGradientLayer ( layer: self . layer)
156
162
gradient. frame = dialogContainer. bounds
157
163
gradient. colors = [ UIColor ( red: 218 / 255 , green: 218 / 255 , blue: 218 / 255 , alpha: 1 ) . CGColor,
158
- UIColor ( red: 233 / 255 , green: 233 / 255 , blue: 233 / 255 , alpha: 1 ) . CGColor,
159
- UIColor ( red: 218 / 255 , green: 218 / 255 , blue: 218 / 255 , alpha: 1 ) . CGColor]
164
+ UIColor ( red: 233 / 255 , green: 233 / 255 , blue: 233 / 255 , alpha: 1 ) . CGColor,
165
+ UIColor ( red: 218 / 255 , green: 218 / 255 , blue: 218 / 255 , alpha: 1 ) . CGColor]
160
166
161
167
let cornerRadius = kDatePickerDialogCornerRadius
162
168
gradient. cornerRadius = cornerRadius
@@ -201,7 +207,7 @@ class DatePickerDialog: UIView {
201
207
private func addButtonsToView( container: UIView ) {
202
208
let buttonWidth = container. bounds. size. width / 2
203
209
204
- self . cancelButton = UIButton . buttonWithType ( UIButtonType . Custom) as UIButton
210
+ self . cancelButton = UIButton ( type : UIButtonType . Custom) as UIButton
205
211
self . cancelButton. frame = CGRectMake (
206
212
0 ,
207
213
container. bounds. size. height - kDatePickerDialogDefaultButtonHeight,
@@ -217,7 +223,7 @@ class DatePickerDialog: UIView {
217
223
self . cancelButton. addTarget ( self , action: " buttonTapped: " , forControlEvents: UIControlEvents . TouchUpInside)
218
224
container. addSubview ( self . cancelButton)
219
225
220
- self . doneButton = UIButton . buttonWithType ( UIButtonType . Custom) as UIButton
226
+ self . doneButton = UIButton ( type : UIButtonType . Custom) as UIButton
221
227
self . doneButton. frame = CGRectMake (
222
228
buttonWidth,
223
229
container. bounds. size. height - kDatePickerDialogDefaultButtonHeight,
@@ -260,4 +266,4 @@ class DatePickerDialog: UIView {
260
266
return CGSizeMake ( screenWidth, screenHeight)
261
267
}
262
268
263
- }
269
+ }
0 commit comments