forked from mapbox/Fingertips
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDSFingerTipWindow.m
363 lines (281 loc) · 10.5 KB
/
DSFingerTipWindow.m
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
// DSFingerTipWindow.m
//
// Created by Justin R. Miller on 3/29/11.
// Copyright 2011-2013 Development Seed. All rights reserved.
//
#import "DSFingerTipWindow.h"
// This file must be built with ARC.
//
#if !__has_feature(objc_arc)
#error "ARC must be enabled for DSFingerTipWindow.m"
#endif
// Turn this on to debug touches during development.
//
#ifdef TARGET_IPHONE_SIMULATOR
#define DEBUG_FINGERTIP_WINDOW 1
#else
#define DEBUG_FINGERTIP_WINDOW 1
#endif
@interface DSFingerTipView : UIImageView
@property (nonatomic, assign) NSTimeInterval timestamp;
@property (nonatomic, assign) BOOL shouldAutomaticallyRemoveAfterTimeout;
@property (nonatomic, assign, getter=isFadingOut) BOOL fadingOut;
@end
#pragma mark -
@interface DSFingerTipWindow ()
@property (nonatomic, strong) UIWindow *overlayWindow;
@property (nonatomic, assign) BOOL active;
@property (nonatomic, assign) BOOL fingerTipRemovalScheduled;
- (void)DSFingerTipWindow_commonInit;
- (BOOL)anyScreenIsMirrored;
- (void)updateFingertipsAreActive;
- (void)scheduleFingerTipRemoval;
- (void)cancelScheduledFingerTipRemoval;
- (void)removeInactiveFingerTips;
- (void)removeFingerTipWithHash:(NSUInteger)hash animated:(BOOL)animated;
- (BOOL)shouldAutomaticallyRemoveFingerTipForTouch:(UITouch *)touch;
@end
#pragma mark -
@implementation DSFingerTipWindow
@synthesize touchImage=_touchImage;
- (id)initWithCoder:(NSCoder *)decoder
{
// This covers NIB-loaded windows.
//
self = [super initWithCoder:decoder];
if (self != nil)
[self DSFingerTipWindow_commonInit];
return self;
}
- (id)initWithFrame:(CGRect)rect
{
// This covers programmatically-created windows.
//
self = [super initWithFrame:rect];
if (self != nil)
[self DSFingerTipWindow_commonInit];
return self;
}
- (void)DSFingerTipWindow_commonInit
{
self.strokeColor = [UIColor blackColor];
self.fillColor = [UIColor blackColor];
self.overlayWindow = [[UIWindow alloc] initWithFrame:self.frame];
self.overlayWindow.userInteractionEnabled = NO;
self.overlayWindow.windowLevel = UIWindowLevelStatusBar;
self.overlayWindow.backgroundColor = [UIColor clearColor];
self.overlayWindow.hidden = NO;
self.touchAlpha = 0.5;
self.fadeDuration = 0.3;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(screenConnect:)
name:UIScreenDidConnectNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(screenDisconnect:)
name:UIScreenDidDisconnectNotification
object:nil];
// Set up active now, in case the screen was present before the window was created (or application launched).
//
[self updateFingertipsAreActive];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIScreenDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIScreenDidDisconnectNotification object:nil];
}
#pragma mark -
- (UIImage *)touchImage
{
if ( ! _touchImage)
{
UIBezierPath *clipPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 50.0, 50.0)];
UIGraphicsBeginImageContextWithOptions(clipPath.bounds.size, NO, 0);
UIBezierPath *drawPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(25.0, 25.0)
radius:22.0
startAngle:0
endAngle:2 * M_PI
clockwise:YES];
drawPath.lineWidth = 2.0;
[self.strokeColor setStroke];
[self.fillColor setFill];
[drawPath stroke];
[drawPath fill];
[clipPath addClip];
_touchImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return _touchImage;
}
#pragma mark -
#pragma mark Screen notifications
- (void)screenConnect:(NSNotification *)notification
{
[self updateFingertipsAreActive];
}
- (void)screenDisconnect:(NSNotification *)notification
{
[self updateFingertipsAreActive];
}
- (BOOL)anyScreenIsMirrored
{
if ( ! [UIScreen instancesRespondToSelector:@selector(mirroredScreen)])
return NO;
for (UIScreen *screen in [UIScreen screens])
{
if ([screen mirroredScreen] != nil)
return YES;
}
return NO;
}
- (void)updateFingertipsAreActive;
{
#if DEBUG_FINGERTIP_WINDOW
self.active = YES;
#else
self.active = [self anyScreenIsMirrored];
#endif
}
#pragma mark -
#pragma mark UIWindow overrides
- (void)sendEvent:(UIEvent *)event
{
if (self.active)
{
NSSet *allTouches = [event allTouches];
for (UITouch *touch in [allTouches allObjects])
{
switch (touch.phase)
{
case UITouchPhaseBegan:
case UITouchPhaseMoved:
case UITouchPhaseStationary:
{
DSFingerTipView *touchView = (DSFingerTipView *)[self.overlayWindow viewWithTag:touch.hash];
if (touch.phase != UITouchPhaseStationary && touchView != nil && [touchView isFadingOut])
{
[touchView removeFromSuperview];
touchView = nil;
}
if (touchView == nil && touch.phase != UITouchPhaseStationary)
{
touchView = [[DSFingerTipView alloc] initWithImage:self.touchImage];
[self.overlayWindow addSubview:touchView];
}
if ( ! [touchView isFadingOut])
{
touchView.alpha = self.touchAlpha;
touchView.center = [touch locationInView:self.overlayWindow];
touchView.tag = touch.hash;
touchView.timestamp = touch.timestamp;
touchView.shouldAutomaticallyRemoveAfterTimeout = [self shouldAutomaticallyRemoveFingerTipForTouch:touch];
}
break;
}
case UITouchPhaseEnded:
case UITouchPhaseCancelled:
{
[self removeFingerTipWithHash:touch.hash animated:YES];
break;
}
}
}
}
[super sendEvent:event];
[self scheduleFingerTipRemoval]; // We may not see all UITouchPhaseEnded/UITouchPhaseCancelled events.
}
#pragma mark -
#pragma mark Private
- (void)scheduleFingerTipRemoval
{
if (self.fingerTipRemovalScheduled)
return;
self.fingerTipRemovalScheduled = YES;
[self performSelector:@selector(removeInactiveFingerTips) withObject:nil afterDelay:0.1];
}
- (void)cancelScheduledFingerTipRemoval
{
self.fingerTipRemovalScheduled = YES;
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(removeInactiveFingerTips) object:nil];
}
- (void)removeInactiveFingerTips
{
self.fingerTipRemovalScheduled = NO;
NSTimeInterval now = [[NSProcessInfo processInfo] systemUptime];
const CGFloat REMOVAL_DELAY = 0.2;
for (DSFingerTipView *touchView in [self.overlayWindow subviews])
{
if ( ! [touchView isKindOfClass:[DSFingerTipView class]])
continue;
if (touchView.shouldAutomaticallyRemoveAfterTimeout && now > touchView.timestamp + REMOVAL_DELAY)
[self removeFingerTipWithHash:touchView.tag animated:YES];
}
if ([[self.overlayWindow subviews] count] > 0)
[self scheduleFingerTipRemoval];
}
- (void)removeFingerTipWithHash:(NSUInteger)hash animated:(BOOL)animated;
{
DSFingerTipView *touchView = (DSFingerTipView *)[self.overlayWindow viewWithTag:hash];
if ( ! [touchView isKindOfClass:[DSFingerTipView class]])
return;
if ([touchView isFadingOut])
return;
BOOL animationsWereEnabled = [UIView areAnimationsEnabled];
if (animated)
{
[UIView setAnimationsEnabled:YES];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:self.fadeDuration];
}
touchView.frame = CGRectMake(touchView.center.x - touchView.frame.size.width,
touchView.center.y - touchView.frame.size.height,
touchView.frame.size.width * 2,
touchView.frame.size.height * 2);
touchView.alpha = 0.0;
if (animated)
{
[UIView commitAnimations];
[UIView setAnimationsEnabled:animationsWereEnabled];
}
touchView.fadingOut = YES;
[touchView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:self.fadeDuration];
}
- (BOOL)shouldAutomaticallyRemoveFingerTipForTouch:(UITouch *)touch;
{
// We don't reliably get UITouchPhaseEnded or UITouchPhaseCancelled
// events via -sendEvent: for certain touch events. Known cases
// include swipe-to-delete on a table view row, and tap-to-cancel
// swipe to delete. We automatically remove their associated
// fingertips after a suitable timeout.
//
// It would be much nicer if we could remove all touch events after
// a suitable time out, but then we'll prematurely remove touch and
// hold events that are picked up by gesture recognizers (since we
// don't use UITouchPhaseStationary touches for those. *sigh*). So we
// end up with this more complicated setup.
UIView *view = [touch view];
view = [view hitTest:[touch locationInView:view] withEvent:nil];
while (view != nil)
{
if ([view isKindOfClass:[UITableViewCell class]])
{
for (UIGestureRecognizer *recognizer in [touch gestureRecognizers])
{
if ([recognizer isKindOfClass:[UISwipeGestureRecognizer class]])
return YES;
}
}
if ([view isKindOfClass:[UITableView class]])
{
if ([[touch gestureRecognizers] count] == 0)
return YES;
}
view = view.superview;
}
return NO;
}
@end
#pragma mark -
@implementation DSFingerTipView
@end