@@ -2,7 +2,6 @@ import Foundation
2
2
import UIKit
3
3
import QuartzCore
4
4
5
-
6
5
class DatePickerDialog : UIView {
7
6
8
7
typealias DatePickerCallback = ( date: NSDate ) -> Void
@@ -37,10 +36,6 @@ class DatePickerDialog: UIView {
37
36
fatalError ( " init(coder:) has not been implemented " )
38
37
}
39
38
40
- deinit {
41
- NSNotificationCenter . defaultCenter ( ) . removeObserver ( self )
42
- }
43
-
44
39
func setupView( ) {
45
40
self . dialogView = createContainerView ( )
46
41
@@ -56,21 +51,15 @@ class DatePickerDialog: UIView {
56
51
self . backgroundColor = UIColor ( red: 0 , green: 0 , blue: 0 , alpha: 0 )
57
52
58
53
self . addSubview ( self . dialogView!)
59
-
60
- UIApplication . sharedApplication ( ) . windows. first!. addSubview ( self )
61
54
}
62
55
63
56
/* Handle device orientation changes */
64
57
func deviceOrientationDidChange( notification: NSNotification ) {
65
- /* TODO */
58
+ close ( ) // For now just close it
66
59
}
67
60
68
61
/* 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 ) {
62
+ func show( title: String , doneButtonTitle: String = " Done " , cancelButtonTitle: String = " Cancel " , defaultDate: NSDate = NSDate ( ) , datePickerMode: UIDatePickerMode = . DateAndTime, callback: DatePickerCallback ) {
74
63
self . titleLabel. text = title
75
64
self . doneButton. setTitle ( doneButtonTitle, forState: . Normal)
76
65
self . cancelButton. setTitle ( cancelButtonTitle, forState: . Normal)
@@ -80,6 +69,12 @@ class DatePickerDialog: UIView {
80
69
self . datePicker. datePickerMode = self . datePickerMode ?? . Date
81
70
self . datePicker. date = self . defaultDate ?? NSDate ( )
82
71
72
+ /* */
73
+ UIApplication . sharedApplication ( ) . windows. first!. addSubview ( self )
74
+ UIApplication . sharedApplication ( ) . windows. first!. endEditing ( true )
75
+
76
+ NSNotificationCenter . defaultCenter ( ) . addObserver ( self , selector: " deviceOrientationDidChange: " , name: UIDeviceOrientationDidChangeNotification, object: nil )
77
+
83
78
/* Anim */
84
79
UIView . animateWithDuration (
85
80
0.2 ,
@@ -96,6 +91,8 @@ class DatePickerDialog: UIView {
96
91
97
92
/* Dialog close animation then cleaning and removing the view from the parent */
98
93
private func close( ) {
94
+ NSNotificationCenter . defaultCenter ( ) . removeObserver ( self )
95
+
99
96
let currentTransform = self . dialogView. layer. transform
100
97
101
98
let startRotation = ( self . valueForKeyPath ( " layer.transform.rotation.z " ) as? NSNumber ) as? Double ?? 0.0
@@ -194,6 +191,7 @@ class DatePickerDialog: UIView {
194
191
self . cancelButton. setTitleColor ( UIColor ( red: 0.2 , green: 0.2 , blue: 0.2 , alpha: 0.5 ) , forState: UIControlState . Highlighted)
195
192
self . cancelButton. titleLabel!. font = UIFont . boldSystemFontOfSize ( 14 )
196
193
self . cancelButton. layer. cornerRadius = kDatePickerDialogCornerRadius
194
+ self . cancelButton. addTarget ( self , action: " buttonTapped: " , forControlEvents: UIControlEvents . TouchUpInside)
197
195
container. addSubview ( self . cancelButton)
198
196
199
197
self . doneButton = UIButton ( type: UIButtonType . Custom) as UIButton
@@ -222,7 +220,7 @@ class DatePickerDialog: UIView {
222
220
223
221
/* Helper function: count and return the screen's size */
224
222
func countScreenSize( ) -> CGSize {
225
- let screenWidth = UIScreen . mainScreen ( ) . bounds . size. width
223
+ let screenWidth = UIScreen . mainScreen ( ) . applicationFrame . size. width
226
224
let screenHeight = UIScreen . mainScreen ( ) . bounds. size. height
227
225
228
226
return CGSizeMake ( screenWidth, screenHeight)
0 commit comments