-
Notifications
You must be signed in to change notification settings - Fork 70
/
COSTouchVisualizerWindow.m
executable file
·328 lines (277 loc) · 12.3 KB
/
COSTouchVisualizerWindow.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
//
// COSTouchVisualizerWindow.m
// TouchVisualizer
//
// Created by Joe Blau on 3/22/14.
// Copyright (c) 2014 conopsys. All rights reserved.
//
#import "COSTouchVisualizerWindow.h"
#import "COSOverlayVisualizerWindow.h"
#import "COSTouchImageView.h"
#import "COSTouchConfig.h"
#import "COSTouchImageFactory.h"
static const NSTimeInterval COSTouchVisualizerWindowRemoveDelay = 0.2;
@interface COSTouchVisualizerWindow ()
@property (nonatomic) UIWindow *overlayWindow;
@property (nonatomic) UIViewController *overlayWindowViewController;
@property (nonatomic) BOOL fingerTipRemovalScheduled;
@property (nonatomic) NSTimer *timer;
@property (nonatomic) NSSet *allTouches;
@property (nonatomic) UIImage *touchImage;
@property (nonatomic) UIImage *rippleImage;
@property (nonatomic) COSTouchConfig *touchContactConfig;
@property (nonatomic) COSTouchConfig *touchRippleConfig;
@end
@implementation COSTouchVisualizerWindow
- (instancetype)initWithFrame:(CGRect)frame
morphEnabled:(BOOL)morphEnabled
touchVisibility:(COSTouchVisualizerWindowTouchVisibility)touchVisibility
contactConfig:(COSTouchConfig *)contactConfig
rippleConfig:(COSTouchConfig *)rippleConfig {
self = [super initWithFrame:frame];
if (self) {
_morphEnabled = morphEnabled;
_touchVisibility = touchVisibility;
_touchContactConfig = contactConfig ?: [[COSTouchConfig alloc] initWithTouchConfigType:COSTouchConfigTpyeContact];
_touchRippleConfig = rippleConfig ?: [[COSTouchConfig alloc] initWithTouchConfigType:COSTouchConfigTpyeRipple];
}
return self;
}
#pragma mark - Touch / Ripple and Images
- (UIImage *)touchImage {
if (!_touchImage) {
_touchImage = [COSTouchImageFactory imageWithTouchConfig:self.touchContactConfig];
}
return _touchImage;
}
- (UIImage *)rippleImage {
if (!_rippleImage) {
_rippleImage = [COSTouchImageFactory imageWithTouchConfig:self.touchRippleConfig];
}
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 {
[super sendEvent:event];
switch (self.touchVisibility) {
case COSTouchVisualizerWindowTouchVisibilityNever:
return;
break;
case COSTouchVisualizerWindowTouchVisibilityRemoteOnly:
case COSTouchVisualizerWindowTouchVisibilityRemoteAndLocal: {
self.allTouches = [event allTouches];
for (UITouch *touch in [self.allTouches allObjects]) {
switch (touch.phase) {
case UITouchPhaseBegan:
case UITouchPhaseMoved: {
// Generate ripples
COSTouchImageView *rippleView = [[COSTouchImageView alloc] initWithImage:self.rippleImage];
[self.overlayWindow addSubview:rippleView];
rippleView.alpha = self.touchRippleConfig.alpha;
rippleView.center = [touch locationInView:self.overlayWindow];
[UIView animateWithDuration:self.touchRippleConfig.fadeDuration
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
[rippleView setAlpha:0.0];
[rippleView setFrame:CGRectInset(rippleView.frame, 25, 25)];
} completion:^(BOOL finished) {
[rippleView removeFromSuperview];
}];
}
case UITouchPhaseStationary: {
COSTouchImageView *touchView = (COSTouchImageView *)[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 = [[COSTouchImageView alloc] initWithImage:self.touchImage];
[self.overlayWindow addSubview:touchView];
if (self.morphEnabled) {
if (self.timer) {
[self.timer invalidate];
}
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.6
target:self
selector:@selector(performMorphWithTouchView:)
userInfo:touchView
repeats:YES];
}
}
if (![touchView isFadingOut]) {
touchView.alpha = self.touchContactConfig.alpha;
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;
}
}
}
break;
}
}
[self scheduleFingerTipRemoval]; // We may not see all UITouchPhaseEnded/UITouchPhaseCancelled events.
}
- (UIWindow *)overlayWindow {
if (!_overlayWindow) {
_overlayWindow = [[COSOverlayVisualizerWindow alloc] initWithFrame:self.frame];
_overlayWindow.userInteractionEnabled = NO;
_overlayWindow.windowLevel = UIWindowLevelStatusBar;
_overlayWindow.backgroundColor = [UIColor clearColor];
_overlayWindow.hidden = NO;
}
return _overlayWindow;
}
#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];
for (COSTouchImageView *touchView in [self.overlayWindow subviews]) {
if (![touchView isKindOfClass:[COSTouchImageView class]]) {
continue;
}
if (touchView.shouldAutomaticallyRemoveAfterTimeout && now > touchView.timestamp + COSTouchVisualizerWindowRemoveDelay) {
[self removeFingerTipWithHash:touchView.tag animated:YES];
}
}
if ([[self.overlayWindow subviews] count]) {
[self scheduleFingerTipRemoval];
}
}
- (void)removeFingerTipWithHash:(NSUInteger)hash animated:(BOOL)animated {
COSTouchImageView *touchView = (COSTouchImageView *)[self.overlayWindow viewWithTag:hash];
if (touchView == nil || [touchView isFadingOut]) {
return;
}
BOOL animationsWereEnabled = [UIView areAnimationsEnabled];
if (animated) {
[UIView setAnimationsEnabled:YES];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:self.touchContactConfig.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.touchContactConfig.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 *touchView = [touch view];
touchView = [touchView hitTest:[touch locationInView:touchView] withEvent:nil];
while (touchView != nil) {
if ([touchView isKindOfClass:[UITableViewCell class]]) {
for (UIGestureRecognizer *recognizer in [touch gestureRecognizers]) {
if ([recognizer isKindOfClass:[UISwipeGestureRecognizer class]])
return YES;
}
}
if ([touchView isKindOfClass:[UITableView class]] &&
[[touch gestureRecognizers] count] == 0) {
return YES;
}
touchView = touchView.superview;
}
return NO;
}
- (void)performMorphWithTouchView:(COSTouchImageView *)touchView {
NSTimeInterval duration = .4;
NSTimeInterval delay = 0;
// Start
touchView.alpha = self.touchContactConfig.alpha;
touchView.transform = CGAffineTransformMakeScale(1, 1);
[UIView animateWithDuration:duration / 4
delay:delay
options:0
animations:^{
// End
touchView.transform = CGAffineTransformMakeScale(1, 1.2);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:duration / 4
delay:0
options:0
animations:^{
// End
touchView.transform = CGAffineTransformMakeScale(1.2, 0.9);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:duration / 4
delay:0
options:0
animations:^{
// End
touchView.transform = CGAffineTransformMakeScale(0.9, 0.9);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:duration / 4
delay:0
options:0
animations:^{
// End
touchView.transform = CGAffineTransformMakeScale(1, 1);
}
completion:^(BOOL finished){
// If there are no touches, remove this morping touch
if (self.allTouches.count == 0) {
[touchView removeFromSuperview];
}
}];
}];
}];
}];
}
@end