-
Notifications
You must be signed in to change notification settings - Fork 10
/
TouchVisualizer.m
336 lines (266 loc) · 11.6 KB
/
TouchVisualizer.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
#import "TouchVisualizer.h"
#pragma mark - Touch Visualizer Window
@interface TouchVisualizerWindow : UIWindow
@end
@implementation TouchVisualizerWindow
// UIKit tries to get the rootViewController from the overlay window.
// Instead, try to find the rootViewController on some other
// application window.
// Fixes problems with status bar hiding, because it considers the
// overlay window a candidate for controlling the status bar.
- (UIViewController *)rootViewController {
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
if (self == window)
continue;
UIViewController *realRootViewController = window.rootViewController;
if (realRootViewController != nil)
return realRootViewController;
}
return [super rootViewController];
}
@end
#pragma mark - Conopsys Touch Spot View
@interface COSTouchSpotView : UIImageView
@property (nonatomic) NSTimeInterval timestamp;
@property (nonatomic) BOOL shouldAutomaticallyRemoveAfterTimeout;
@property (nonatomic, getter=isFadingOut) BOOL fadingOut;
@end
@implementation COSTouchSpotView
@end
#pragma mark - Conopsys Touch Visualizer Window
@interface TouchVisualizer ()
@property (nonatomic) UIWindow *overlayWindow;
@property (nonatomic) UIViewController *overlayWindowViewController;
@property (nonatomic) BOOL fingerTipRemovalScheduled;
@property (nonatomic) NSTimer *timer;
@property (nonatomic) NSSet *allTouches;
- (void)TouchVisualizer_commonInit;
- (void)scheduleFingerTipRemoval;
- (void)cancelScheduledFingerTipRemoval;
- (void)removeInactiveFingerTips;
- (void)removeFingerTipWithHash:(NSUInteger)hash animated:(BOOL)animated;
- (BOOL)shouldAutomaticallyRemoveFingerTipForTouch:(UITouch *)touch;
@end
@implementation TouchVisualizer
- (id)initWithCoder:(NSCoder *)decoder {
// This covers NIB-loaded windows.
if (self = [super initWithCoder:decoder])
[self TouchVisualizer_commonInit];
return self;
}
- (id)initWithFrame:(CGRect)rect {
// This covers programmatically-created windows.
if (self = [super initWithFrame:rect])
[self TouchVisualizer_commonInit];
return self;
}
- (void)TouchVisualizer_commonInit {
self.strokeColor = [UIColor lightGrayColor];
self.fillColor = [UIColor whiteColor];
self.rippleStrokeColor = [UIColor whiteColor];
self.rippleFillColor = [UIColor blueColor];
self.touchAlpha = 0.5;
self.fadeDuration = 0.3;
self.rippleAlpha = 0.0;
self.rippleFadeDuration = 0.0;
}
#pragma mark - Touch / Ripple and Images
- (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 = 1.0;
[self.strokeColor setStroke];
[self.fillColor setFill];
[drawPath stroke];
[drawPath fill];
[clipPath addClip];
_touchImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return _touchImage;
}
- (UIImage *)rippleImage {
if (!_rippleImage) {
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.rippleStrokeColor setStroke];
[self.rippleFillColor setFill];
[drawPath stroke];
[drawPath fill];
[clipPath addClip];
_rippleImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return _rippleImage;
}
#pragma mark - Active
- (BOOL)anyScreenIsMirrored {
if (![UIScreen instancesRespondToSelector:@selector(mirroredScreen)])
return NO;
for (UIScreen *screen in [UIScreen screens]) {
if ([screen mirroredScreen] != nil) {
return YES;
}
}
return NO;
}
#pragma mark - UIWindow overrides
- (void)sendEvent:(UIEvent *)event {
self.allTouches = [event allTouches];
for (UITouch *touch in [self.allTouches allObjects]) {
switch (touch.phase) {
case UITouchPhaseBegan:
case UITouchPhaseMoved: {
// Generate ripples
COSTouchSpotView *rippleView = [[COSTouchSpotView alloc] initWithImage:self.rippleImage];
[self.overlayWindow addSubview:rippleView];
rippleView.alpha = self.rippleAlpha;
rippleView.center = [touch locationInView:self.overlayWindow];
[UIView animateWithDuration:self.rippleFadeDuration
delay:0.0
options:UIViewAnimationOptionCurveEaseIn // See other
// options
animations:^{
[rippleView setAlpha:0.0];
[rippleView setFrame:CGRectInset(rippleView.frame, 25, 25)];
} completion:^(BOOL finished) {
[rippleView removeFromSuperview];
}];
}
case UITouchPhaseStationary: {
COSTouchSpotView *touchView = (COSTouchSpotView *)[self.overlayWindow viewWithTag:touch.hash];
if (touch.phase != UITouchPhaseStationary && touchView != nil && [touchView isFadingOut]) {
[self.timer invalidate];
[touchView removeFromSuperview];
touchView = nil;
}
if (touchView == nil && touch.phase != UITouchPhaseStationary) {
touchView = [[COSTouchSpotView 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 - Private
- (UIWindow *)overlayWindow {
if (!_overlayWindow) {
_overlayWindow = [[TouchVisualizerWindow alloc] initWithFrame:self.frame];
_overlayWindow.userInteractionEnabled = NO;
_overlayWindow.windowLevel = UIWindowLevelStatusBar;
_overlayWindow.backgroundColor = [UIColor clearColor];
_overlayWindow.hidden = NO;
}
return _overlayWindow;
}
- (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 (COSTouchSpotView *touchView in [self.overlayWindow subviews]) {
if (![touchView isKindOfClass:[COSTouchSpotView class]])
continue;
if (touchView.shouldAutomaticallyRemoveAfterTimeout && now > touchView.timestamp + REMOVAL_DELAY)
[self removeFingerTipWithHash:touchView.tag animated:YES];
}
if ([[self.overlayWindow subviews] count])
[self scheduleFingerTipRemoval];
}
- (void)removeFingerTipWithHash:(NSUInteger)hash animated:(BOOL)animated {
COSTouchSpotView *touchView = (COSTouchSpotView *)[self.overlayWindow viewWithTag:hash];
if (touchView == nil)
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;
}
RCT_EXPORT_MODULE()
@end