1
1
/*
2
- Copyright (C) 2015 Apple Inc. All Rights Reserved.
2
+ Copyright (C) 2016 Apple Inc. All Rights Reserved.
3
3
See LICENSE.txt for this sample’s licensing information
4
4
5
5
*/
@@ -20,12 +20,6 @@ import Foundation
20
20
*/
21
21
class RequestLocationInterfaceController : WKInterfaceController , CLLocationManagerDelegate {
22
22
// MARK: Properties
23
-
24
- /**
25
- When this timer times out, the labels in the interface reset to a default
26
- state that does not resemble a requestLocation result.
27
- */
28
- var interfaceResetTimer = NSTimer ( )
29
23
30
24
/// Location manager to request authorization and location updates.
31
25
let manager = CLLocationManager ( )
@@ -36,9 +30,6 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
36
30
/// Button to request location. Also allows cancelling the location request.
37
31
@IBOutlet var requestLocationButton : WKInterfaceButton !
38
32
39
- /// Timer to count down 5 seconds as a visual cue that the interface will reset.
40
- @IBOutlet var displayTimer : WKInterfaceTimer !
41
-
42
33
/// Label to display the most recent location's latitude.
43
34
@IBOutlet var latitudeLabel : WKInterfaceLabel !
44
35
@@ -70,29 +61,19 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
70
61
return NSLocalizedString ( " Unexpected authorization status. " , comment: " Text to indicate authorization status is an unexpected value " )
71
62
}
72
63
73
- var latitudeResetText : String {
74
- return NSLocalizedString ( " <latitude reset> " , comment: " String indicating that no latitude is shown to the user due to a timer reset " )
75
- }
76
-
77
- var longitudeResetText : String {
78
- return NSLocalizedString ( " <longitude reset> " , comment: " String indicating that no longitude is shown to the user due to a timer reset " )
79
- }
80
-
81
- var errorResetText : String {
82
- return NSLocalizedString ( " <no error> " , comment: " String indicating that no error is shown to the user " )
83
- }
84
-
85
64
// MARK: Interface Controller
86
65
87
66
override func awakeWithContext( context: AnyObject ? ) {
88
67
super. awakeWithContext ( context)
89
68
90
- self . setTitle ( interfaceTitle)
69
+ setTitle ( interfaceTitle)
91
70
92
71
// Remember to set the location manager's delegate.
93
72
manager. delegate = self
94
-
95
- resetInterface ( )
73
+
74
+ latitudeLabel. setAlpha ( 0 )
75
+ longitudeLabel. setAlpha ( 0 )
76
+ errorLabel. setAlpha ( 0 )
96
77
}
97
78
98
79
/// MARK - Button Actions
@@ -127,12 +108,14 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
127
108
manager. requestLocation ( )
128
109
129
110
case . Denied:
111
+ errorLabel. setAlpha ( 1 )
130
112
errorLabel. setText ( deniedText)
131
- restartTimers ( )
113
+ simulateFadeOut ( errorLabel )
132
114
133
115
default :
116
+ errorLabel. setAlpha ( 1 )
134
117
errorLabel. setText ( unexpectedText)
135
- restartTimers ( )
118
+ simulateFadeOut ( errorLabel )
136
119
}
137
120
}
138
121
@@ -142,21 +125,27 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
142
125
When the location manager receives new locations, display the latitude and
143
126
longitude of the latest location and restart the timers.
144
127
*/
145
- func locationManager( manager: CLLocationManager , didUpdateLocations locations: [ AnyObject ] ) {
128
+ func locationManager( manager: CLLocationManager , didUpdateLocations locations: [ CLLocation ] ) {
146
129
guard !locations. isEmpty else { return }
147
130
148
131
dispatch_async ( dispatch_get_main_queue ( ) ) {
149
- let lastLocationCoordinate = locations. last!. coordinate!
150
-
132
+ let lastLocationCoordinate = locations. last!. coordinate
133
+
151
134
self . latitudeLabel. setText ( String ( lastLocationCoordinate. latitude) )
152
135
153
136
self . longitudeLabel. setText ( String ( lastLocationCoordinate. longitude) )
154
137
138
+ self . latitudeLabel. setAlpha ( 1 )
139
+
140
+ self . longitudeLabel. setAlpha ( 1 )
141
+
155
142
self . isRequestingLocation = false
156
143
157
144
self . requestLocationButton. setTitle ( self . requestLocationTitle)
158
145
159
- self . restartTimers ( )
146
+ self . simulateFadeOut ( self . latitudeLabel)
147
+
148
+ self . simulateFadeOut ( self . longitudeLabel)
160
149
}
161
150
}
162
151
@@ -166,13 +155,15 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
166
155
*/
167
156
func locationManager( manager: CLLocationManager , didFailWithError error: NSError ) {
168
157
dispatch_async ( dispatch_get_main_queue ( ) ) {
158
+ self . errorLabel. setAlpha ( 1 )
159
+
169
160
self . errorLabel. setText ( String ( error. localizedDescription) )
170
161
171
162
self . isRequestingLocation = false
172
163
173
164
self . requestLocationButton. setTitle ( self . requestLocationTitle)
174
165
175
- self . restartTimers ( )
166
+ self . simulateFadeOut ( self . errorLabel )
176
167
}
177
168
}
178
169
@@ -189,63 +180,39 @@ class RequestLocationInterfaceController: WKInterfaceController, CLLocationManag
189
180
manager. requestLocation ( )
190
181
191
182
case . Denied:
183
+ self . errorLabel. setAlpha ( 1 )
192
184
self . errorLabel. setText ( self . deniedText)
193
185
self . isRequestingLocation = false
194
186
self . requestLocationButton. setTitle ( self . requestLocationTitle)
195
- self . restartTimers ( )
187
+ self . simulateFadeOut ( self . errorLabel )
196
188
197
189
default :
190
+ self . errorLabel. setAlpha ( 1 )
198
191
self . errorLabel. setText ( self . unexpectedText)
199
192
self . isRequestingLocation = false
200
193
self . requestLocationButton. setTitle ( self . requestLocationTitle)
201
- self . restartTimers ( )
194
+ self . simulateFadeOut ( self . errorLabel )
202
195
}
203
196
}
204
197
}
205
198
206
199
/// MARK - Resetting
207
200
208
201
/**
209
- Resets the text labels in the interface to empty labels.
210
-
211
- This method is useful for cleaning the interface to ensure that data
212
- displayed to the user is not stale.
202
+ Simulates fading out animation by setting the alpha of the given label to
203
+ progressively smaller numbers.
213
204
*/
214
- func resetInterface( ) {
215
- dispatch_async ( dispatch_get_main_queue ( ) ) {
216
- self . stopDisplayTimer ( )
217
-
218
- self . latitudeLabel. setText ( self . latitudeResetText)
219
-
220
- self . longitudeLabel. setText ( self . longitudeResetText)
221
-
222
- self . errorLabel. setText ( self . errorResetText)
223
- }
224
- }
225
-
226
- /**
227
- Restarts the NSTimer and the WKInterface timer by stopping / invalidating
228
- them, then starting them with a 5 second timeout.
229
- */
230
- func restartTimers( ) {
231
- stopDisplayTimer ( )
232
-
233
- interfaceResetTimer. invalidate ( )
234
-
235
- interfaceResetTimer = NSTimer . scheduledTimerWithTimeInterval ( 5 , target: self , selector: " resetInterface " , userInfo: [ : ] , repeats: false )
236
-
237
- let fiveSecondDelay = NSDate ( timeIntervalSinceNow: 5 )
205
+ func simulateFadeOut( label: WKInterfaceLabel ) {
206
+ let mainQueue = dispatch_get_main_queue ( )
238
207
239
- displayTimer. setDate ( fiveSecondDelay)
240
-
241
- displayTimer. start ( )
242
- }
243
-
244
- /// Stops the display timer.
245
- func stopDisplayTimer( ) {
246
- let now = NSDate ( )
247
- displayTimer. setDate ( now)
208
+ for index in 1 ... 10 {
209
+ let time = dispatch_time ( DISPATCH_TIME_NOW, Int64 ( Double ( index) / 10.0 * Double( NSEC_PER_SEC) ) )
210
+
211
+ dispatch_after ( time, mainQueue) {
212
+ let alphaAmount = CGFloat ( 1 - ( 0.1 * Float( index) ) )
248
213
249
- displayTimer. stop ( )
214
+ label. setAlpha ( alphaAmount)
215
+ }
216
+ }
250
217
}
251
218
}
0 commit comments