1
- //
2
- // DatePicker.swift
3
-
4
1
import Foundation
5
2
import UIKit
6
3
import QuartzCore
7
4
8
5
9
6
class DatePickerDialog : UIView {
10
7
8
+ typealias DatePickerCallback = ( date: NSDate ) -> Void
9
+
11
10
/* Consts */
12
- private let kDatePickerDialogDefaultButtonHeight : CGFloat = 50
11
+ private let kDatePickerDialogDefaultButtonHeight : CGFloat = 50
13
12
private let kDatePickerDialogDefaultButtonSpacerHeight : CGFloat = 1
14
- private let kDatePickerDialogCornerRadius : CGFloat = 7
15
- private let kDatePickerDialogCancelButtonTag : Int = 1
16
- private let kDatePickerDialogDoneButtonTag : Int = 2
13
+ private let kDatePickerDialogCornerRadius : CGFloat = 7
14
+ private let kDatePickerDialogDoneButtonTag : Int = 1
17
15
18
16
/* Views */
19
- private var dialogView : UIView !
20
- private var titleLabel : UILabel !
21
- private var datePicker : UIDatePicker !
17
+ private var dialogView : UIView !
18
+ private var titleLabel : UILabel !
19
+ private var datePicker : UIDatePicker !
22
20
private var cancelButton : UIButton !
23
- private var doneButton : UIButton !
21
+ private var doneButton : UIButton !
24
22
25
23
/* Vars */
26
- private var title : String !
27
- private var doneButtonTitle : String !
28
- private var cancelButtonTitle : String !
29
- private var defaultDate : NSDate !
30
- private var datePickerMode : UIDatePickerMode !
31
- private var callback : ( ( date: NSDate ) -> Void ) !
24
+ private var defaultDate : NSDate ?
25
+ private var datePickerMode : UIDatePickerMode ?
26
+ private var callback : DatePickerCallback ?
32
27
33
28
34
29
/* Overrides */
35
30
init ( ) {
36
31
super. init ( frame: CGRectMake ( 0 , 0 , UIScreen . mainScreen ( ) . bounds. size. width, UIScreen . mainScreen ( ) . bounds. size. height) )
37
32
38
- NSNotificationCenter . defaultCenter ( ) . addObserver ( self , selector : " deviceOrientationDidChange: " , name : UIDeviceOrientationDidChangeNotification , object : nil )
33
+ setupView ( )
39
34
}
40
35
41
36
required init ( coder aDecoder: NSCoder ) {
42
37
fatalError ( " init(coder:) has not been implemented " )
43
38
}
44
39
45
- /* Handle device orientation changes */
46
- func deviceOrientationDidChange( notification: NSNotification ) {
47
- /* TODO */
40
+ deinit {
41
+ NSNotificationCenter . defaultCenter ( ) . removeObserver ( self )
48
42
}
49
43
50
- /* Create the dialog view, and animate opening the dialog */
51
- func show( title: String , datePickerMode: UIDatePickerMode = . DateAndTime, callback: ( ( date: NSDate ) -> Void ) ) {
52
- show ( title, doneButtonTitle: " Done " , cancelButtonTitle: " Cancel " , datePickerMode: datePickerMode, callback: callback)
53
- }
54
-
55
- func show( title: String , doneButtonTitle: String , cancelButtonTitle: String , defaultDate: NSDate = NSDate ( ) , datePickerMode: UIDatePickerMode = . DateAndTime, callback: ( ( date: NSDate ) -> Void ) ) {
56
- self . title = title
57
- self . doneButtonTitle = doneButtonTitle
58
- self . cancelButtonTitle = cancelButtonTitle
59
- self . datePickerMode = datePickerMode
60
- self . callback = callback
61
- self . defaultDate = defaultDate
62
-
44
+ func setupView( ) {
63
45
self . dialogView = createContainerView ( )
64
-
46
+
65
47
self . dialogView!. layer. shouldRasterize = true
66
48
self . dialogView!. layer. rasterizationScale = UIScreen . mainScreen ( ) . scale
67
-
49
+
68
50
self . layer. shouldRasterize = true
69
51
self . layer. rasterizationScale = UIScreen . mainScreen ( ) . scale
70
-
52
+
71
53
self . dialogView!. layer. opacity = 0.5
72
54
self . dialogView!. layer. transform = CATransform3DMakeScale ( 1.3 , 1.3 , 1 )
73
-
55
+
74
56
self . backgroundColor = UIColor ( red: 0 , green: 0 , blue: 0 , alpha: 0 )
75
-
57
+
76
58
self . addSubview ( self . dialogView!)
77
59
78
- /* Attached to the top most window (make sure we are using the right orientation) */
79
- let interfaceOrientation = UIApplication . sharedApplication ( ) . statusBarOrientation
80
-
81
- switch ( interfaceOrientation) {
82
- case UIInterfaceOrientation . LandscapeLeft:
83
- let t : Double = M_PI * 270 / 180
84
- self . transform = CGAffineTransformMakeRotation ( CGFloat ( t) )
85
- break
86
-
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
96
-
97
- default :
98
- break
99
- }
100
-
101
- self . frame = CGRectMake ( 0 , 0 , self . frame. width, self . frame. size. height)
102
60
UIApplication . sharedApplication ( ) . windows. first!. addSubview ( self )
61
+ }
62
+
63
+ /* Handle device orientation changes */
64
+ func deviceOrientationDidChange( notification: NSNotification ) {
65
+ /* TODO */
66
+ }
67
+
68
+ /* Create the dialog view, and animate opening the dialog */
69
+ func show( title: String , datePickerMode: UIDatePickerMode = . DateAndTime, callback: DatePickerCallback ) {
70
+ show ( title, doneButtonTitle: " Done " , cancelButtonTitle: " Cancel " , datePickerMode: datePickerMode, callback: callback)
71
+ }
72
+
73
+ func show( title: String , doneButtonTitle: String , cancelButtonTitle: String , defaultDate: NSDate = NSDate ( ) , datePickerMode: UIDatePickerMode = . DateAndTime, callback: DatePickerCallback ) {
74
+ self . titleLabel. text = title
75
+ self . doneButton. setTitle ( doneButtonTitle, forState: . Normal)
76
+ self . cancelButton. setTitle ( cancelButtonTitle, forState: . Normal)
77
+ self . datePickerMode = datePickerMode
78
+ self . callback = callback
79
+ self . defaultDate = defaultDate
80
+ self . datePicker. datePickerMode = self . datePickerMode ?? . Date
81
+ self . datePicker. date = self . defaultDate ?? NSDate ( )
103
82
104
83
/* Anim */
105
84
UIView . animateWithDuration (
@@ -187,14 +166,11 @@ class DatePickerDialog: UIView {
187
166
self . titleLabel = UILabel ( frame: CGRectMake ( 10 , 10 , 280 , 30 ) )
188
167
self . titleLabel. textAlignment = NSTextAlignment . Center
189
168
self . titleLabel. font = UIFont . boldSystemFontOfSize ( 17 )
190
- self . titleLabel. text = self . title
191
169
dialogContainer. addSubview ( self . titleLabel)
192
170
193
171
self . datePicker = UIDatePicker ( frame: CGRectMake ( 0 , 30 , 0 , 0 ) )
194
172
self . datePicker. autoresizingMask = UIViewAutoresizing . FlexibleRightMargin
195
173
self . datePicker. frame. size. width = 300
196
- self . datePicker. datePickerMode = self . datePickerMode
197
- self . datePicker. date = self . defaultDate
198
174
dialogContainer. addSubview ( self . datePicker)
199
175
200
176
// Add the buttons
@@ -214,13 +190,10 @@ class DatePickerDialog: UIView {
214
190
buttonWidth,
215
191
kDatePickerDialogDefaultButtonHeight
216
192
)
217
- self . cancelButton. tag = kDatePickerDialogCancelButtonTag
218
- self . cancelButton. setTitle ( self . cancelButtonTitle, forState: UIControlState . Normal)
219
193
self . cancelButton. setTitleColor ( UIColor ( red: 0 , green: 0.5 , blue: 1 , alpha: 1 ) , forState: UIControlState . Normal)
220
194
self . cancelButton. setTitleColor ( UIColor ( red: 0.2 , green: 0.2 , blue: 0.2 , alpha: 0.5 ) , forState: UIControlState . Highlighted)
221
195
self . cancelButton. titleLabel!. font = UIFont . boldSystemFontOfSize ( 14 )
222
196
self . cancelButton. layer. cornerRadius = kDatePickerDialogCornerRadius
223
- self . cancelButton. addTarget ( self , action: " buttonTapped: " , forControlEvents: UIControlEvents . TouchUpInside)
224
197
container. addSubview ( self . cancelButton)
225
198
226
199
self . doneButton = UIButton ( type: UIButtonType . Custom) as UIButton
@@ -231,7 +204,6 @@ class DatePickerDialog: UIView {
231
204
kDatePickerDialogDefaultButtonHeight
232
205
)
233
206
self . doneButton. tag = kDatePickerDialogDoneButtonTag
234
- self . doneButton. setTitle ( self . doneButtonTitle, forState: UIControlState . Normal)
235
207
self . doneButton. setTitleColor ( UIColor ( red: 0 , green: 0.5 , blue: 1 , alpha: 1 ) , forState: UIControlState . Normal)
236
208
self . doneButton. setTitleColor ( UIColor ( red: 0.2 , green: 0.2 , blue: 0.2 , alpha: 0.5 ) , forState: UIControlState . Highlighted)
237
209
self . doneButton. titleLabel!. font = UIFont . boldSystemFontOfSize ( 14 )
@@ -242,26 +214,16 @@ class DatePickerDialog: UIView {
242
214
243
215
func buttonTapped( sender: UIButton ! ) {
244
216
if sender. tag == kDatePickerDialogDoneButtonTag {
245
- self . callback ( date: self . datePicker. date)
246
- } else if sender. tag == kDatePickerDialogCancelButtonTag {
247
- //There's nothing do to here \o\
217
+ self . callback ? ( date: self . datePicker. date)
248
218
}
249
219
250
220
close ( )
251
221
}
252
222
253
223
/* Helper function: count and return the screen's size */
254
224
func countScreenSize( ) -> CGSize {
255
- var screenWidth = UIScreen . mainScreen ( ) . bounds. size. width
256
- var screenHeight = UIScreen . mainScreen ( ) . bounds. size. height
257
-
258
- let interfaceOrientaion = UIApplication . sharedApplication ( ) . statusBarOrientation
259
-
260
- if UIInterfaceOrientationIsLandscape ( interfaceOrientaion) {
261
- let tmp = screenWidth
262
- screenWidth = screenHeight
263
- screenHeight = tmp
264
- }
225
+ let screenWidth = UIScreen . mainScreen ( ) . bounds. size. width
226
+ let screenHeight = UIScreen . mainScreen ( ) . bounds. size. height
265
227
266
228
return CGSizeMake ( screenWidth, screenHeight)
267
229
}
0 commit comments