forked from kif-framework/KIF
-
Notifications
You must be signed in to change notification settings - Fork 3
/
UIView-KIFAdditions.m
540 lines (428 loc) · 18.3 KB
/
UIView-KIFAdditions.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
//
// UIView-KIFAdditions.m
// KIF
//
// Created by Eric Firestone on 5/20/11.
// Licensed to Square, Inc. under one or more contributor license agreements.
// See the LICENSE file distributed with this work for the terms under
// which Square, Inc. licenses this file to you.
#import "UIView-KIFAdditions.h"
#import "CGGeometry-KIFAdditions.h"
#import "UIAccessibilityElement-KIFAdditions.h"
#import "UIApplication-KIFAdditions.h"
#import "UITouch-KIFAdditions.h"
#import <objc/runtime.h>
typedef struct __GSEvent * GSEventRef;
//
// GSEvent is an undeclared object. We don't need to use it ourselves but some
// Apple APIs (UIScrollView in particular) require the x and y fields to be present.
//
@interface KIFEventProxy : NSObject
{
@public
unsigned int flags;
unsigned int type;
unsigned int ignored1;
float x1;
float y1;
float x2;
float y2;
unsigned int ignored2[10];
unsigned int ignored3[7];
float sizeX;
float sizeY;
float x3;
float y3;
unsigned int ignored4[3];
}
@end
@implementation KIFEventProxy
@end
// Exposes methods of UITouchesEvent so that the compiler doesn't complain
@interface UIEvent (KIFAdditionsPrivate)
- (void)_addTouch:(id)arg1 forDelayedDelivery:(BOOL)arg2;
- (void)_clearTouches;
- (void)_setGSEvent:(GSEventRef)event;
@end
@interface NSObject (UIWebDocumentViewInternal)
- (void)tapInteractionWithLocation:(CGPoint)point;
@end
@interface UIView (KIFAdditionsPrivate)
- (UIEvent *)_eventWithTouch:(UITouch *)touch;
@end
@implementation UIView (KIFAdditions)
- (UIAccessibilityElement *)accessibilityElementWithLabel:(NSString *)label
{
return [self accessibilityElementWithLabel:label traits:UIAccessibilityTraitNone];
}
- (UIAccessibilityElement *)accessibilityElementWithLabel:(NSString *)label traits:(UIAccessibilityTraits)traits;
{
return [self accessibilityElementWithLabel:label accessibilityValue:nil traits:traits];
}
- (UIAccessibilityElement *)accessibilityElementWithLabel:(NSString *)label accessibilityValue:(NSString *)value traits:(UIAccessibilityTraits)traits;
{
return [self accessibilityElementMatchingBlock:^(UIAccessibilityElement *element) {
// TODO: This is a temporary fix for an SDK defect.
NSString *accessibilityValue = element.accessibilityValue;
if ([accessibilityValue isKindOfClass:[NSAttributedString class]]) {
accessibilityValue = [(NSAttributedString *)accessibilityValue string];
}
BOOL labelsMatch = [element.accessibilityLabel isEqual:label];
BOOL traitsMatch = ((element.accessibilityTraits) & traits) == traits;
BOOL valuesMatch = !value || [value isEqual:accessibilityValue];
return (BOOL)(labelsMatch && traitsMatch && valuesMatch);
}];
}
- (UIAccessibilityElement *)accessibilityElementMatchingBlock:(BOOL(^)(UIAccessibilityElement *))matchBlock;
{
if (self.hidden) {
return nil;
}
// In case multiple elements with the same label exist, prefer ones that are currently visible
UIAccessibilityElement *matchingButOccludedElement = nil;
BOOL elementMatches = matchBlock((UIAccessibilityElement *)self);
if (elementMatches) {
if (self.isTappable) {
return (UIAccessibilityElement *)self;
} else {
matchingButOccludedElement = (UIAccessibilityElement *)self;
}
}
// Check the subviews first. Even if the receiver says it's an accessibility container,
// the returned objects are UIAccessibilityElementMockViews (which aren't actually views)
// rather than the real subviews it contains. We want the real views if possible.
// UITableViewCell is such an offender.
for (UIView *view in [self.subviews reverseObjectEnumerator]) {
UIAccessibilityElement *element = [view accessibilityElementMatchingBlock:matchBlock];
if (!element) {
continue;
}
UIView *viewForElement = [UIAccessibilityElement viewContainingAccessibilityElement:element];
CGRect accessibilityFrame = [viewForElement.window convertRect:element.accessibilityFrame toView:viewForElement];
if ([viewForElement isTappableInRect:accessibilityFrame]) {
return element;
} else {
matchingButOccludedElement = element;
}
}
NSMutableArray *elementStack = [NSMutableArray arrayWithObject:self];
while (elementStack.count) {
UIAccessibilityElement *element = [elementStack lastObject];
[elementStack removeLastObject];
BOOL elementMatches = matchBlock(element);
if (elementMatches) {
UIView *viewForElement = [UIAccessibilityElement viewContainingAccessibilityElement:element];
CGRect accessibilityFrame = [viewForElement.window convertRect:element.accessibilityFrame toView:viewForElement];
if ([viewForElement isTappableInRect:accessibilityFrame]) {
return element;
} else {
matchingButOccludedElement = element;
continue;
}
}
// If the view is an accessibility container, and we didn't find a matching subview,
// then check the actual accessibility elements
NSInteger accessibilityElementCount = element.accessibilityElementCount;
if (accessibilityElementCount == 0 || accessibilityElementCount == NSNotFound) {
continue;
}
for (NSInteger accessibilityElementIndex = 0; accessibilityElementIndex < accessibilityElementCount; accessibilityElementIndex++) {
UIAccessibilityElement *subelement = [element accessibilityElementAtIndex:accessibilityElementIndex];
if (subelement) {
[elementStack addObject:subelement];
}
}
}
return matchingButOccludedElement;
}
- (UIView *)subviewWithClassNamePrefix:(NSString *)prefix;
{
NSArray *subviews = [self subviewsWithClassNamePrefix:prefix];
if ([subviews count] == 0) {
return nil;
}
return subviews[0];
}
- (NSArray *)subviewsWithClassNamePrefix:(NSString *)prefix;
{
NSMutableArray *result = [NSMutableArray array];
// Breadth-first population of matching subviews
// First traverse the next level of subviews, adding matches.
for (UIView *view in self.subviews) {
if ([NSStringFromClass([view class]) hasPrefix:prefix]) {
[result addObject:view];
}
}
// Now traverse the subviews of the subviews, adding matches.
for (UIView *view in self.subviews) {
NSArray *matchingSubviews = [view subviewsWithClassNamePrefix:prefix];
[result addObjectsFromArray:matchingSubviews];
}
return result;
}
- (UIView *)subviewWithClassNameOrSuperClassNamePrefix:(NSString *)prefix;
{
NSArray *subviews = [self subviewsWithClassNameOrSuperClassNamePrefix:prefix];
if ([subviews count] == 0) {
return nil;
}
return subviews[0];
}
- (NSArray *)subviewsWithClassNameOrSuperClassNamePrefix:(NSString *)prefix;
{
NSMutableArray * result = [NSMutableArray array];
// Breadth-first population of matching subviews
// First traverse the next level of subviews, adding matches
for (UIView *view in self.subviews) {
Class klass = [view class];
while (klass) {
if ([NSStringFromClass(klass) hasPrefix:prefix]) {
[result addObject:view];
break;
}
klass = [klass superclass];
}
}
// Now traverse the subviews of the subviews, adding matches
for (UIView *view in self.subviews) {
NSArray * matchingSubviews = [view subviewsWithClassNameOrSuperClassNamePrefix:prefix];
[result addObjectsFromArray:matchingSubviews];
}
return result;
}
- (BOOL)isDescendantOfFirstResponder;
{
if ([self isFirstResponder]) {
return YES;
}
return [self.superview isDescendantOfFirstResponder];
}
- (void)flash;
{
UIColor *originalBackgroundColor = [self.backgroundColor retain];
for (NSUInteger i = 0; i < 5; i++) {
self.backgroundColor = [UIColor yellowColor];
CFRunLoopRunInMode(kCFRunLoopDefaultMode, .05, false);
self.backgroundColor = [UIColor blueColor];
CFRunLoopRunInMode(kCFRunLoopDefaultMode, .05, false);
}
self.backgroundColor = originalBackgroundColor;
[originalBackgroundColor release];
}
- (void)tap;
{
CGPoint centerPoint = CGPointMake(self.frame.size.width * 0.5f, self.frame.size.height * 0.5f);
[self tapAtPoint:centerPoint];
}
- (void)tapAtPoint:(CGPoint)point;
{
// Web views don't handle touches in a normal fashion, but they do have a method we can call to tap them
// This may not be necessary anymore. We didn't properly support controls that used gesture recognizers
// when this was added, but we now do. It needs to be tested before we can get rid of it.
id /*UIWebBrowserView*/ webBrowserView = nil;
if ([NSStringFromClass([self class]) isEqual:@"UIWebBrowserView"]) {
webBrowserView = self;
} else if ([self isKindOfClass:[UIWebView class]]) {
id webViewInternal = nil;
object_getInstanceVariable(self, "_internal", (void **)&webViewInternal);
object_getInstanceVariable(webViewInternal, "browserView", (void **)&webBrowserView);
}
if (webBrowserView) {
[webBrowserView tapInteractionWithLocation:point];
return;
}
// Handle touches in the normal way for other views
UITouch *touch = [[UITouch alloc] initAtPoint:point inView:self];
[touch setPhase:UITouchPhaseBegan];
UIEvent *event = [self _eventWithTouch:touch];
[[UIApplication sharedApplication] sendEvent:event];
[touch setPhase:UITouchPhaseEnded];
[[UIApplication sharedApplication] sendEvent:event];
// Dispatching the event doesn't actually update the first responder, so fake it
if ([touch.view isDescendantOfView:self] && [self canBecomeFirstResponder]) {
[self becomeFirstResponder];
}
[touch release];
}
#define DRAG_TOUCH_DELAY 0.01
- (void)longPressAtPoint:(CGPoint)point duration:(NSTimeInterval)duration
{
UITouch *touch = [[UITouch alloc] initAtPoint:point inView:self];
[touch setPhase:UITouchPhaseBegan];
UIEvent *eventDown = [self _eventWithTouch:touch];
[[UIApplication sharedApplication] sendEvent:eventDown];
CFRunLoopRunInMode(kCFRunLoopDefaultMode, DRAG_TOUCH_DELAY, false);
for (NSTimeInterval timeSpent = DRAG_TOUCH_DELAY; timeSpent < duration; timeSpent += DRAG_TOUCH_DELAY)
{
[touch setPhase:UITouchPhaseStationary];
UIEvent *eventStillDown = [self _eventWithTouch:touch];
[[UIApplication sharedApplication] sendEvent:eventStillDown];
CFRunLoopRunInMode(kCFRunLoopDefaultMode, DRAG_TOUCH_DELAY, false);
}
[touch setPhase:UITouchPhaseEnded];
UIEvent *eventUp = [self _eventWithTouch:touch];
[[UIApplication sharedApplication] sendEvent:eventUp];
// Dispatching the event doesn't actually update the first responder, so fake it
if ([touch.view isDescendantOfView:self] && [self canBecomeFirstResponder]) {
[self becomeFirstResponder];
}
[touch release];
}
- (void)dragFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint;
{
// Handle touches in the normal way for other views
CGPoint points[] = {startPoint, CGPointMidPoint(startPoint, endPoint), endPoint};
[self dragAlongPathWithPoints:points count:sizeof(points) / sizeof(CGPoint)];
}
- (void)dragAlongPathWithPoints:(CGPoint *)points count:(NSInteger)count;
{
// we need at least two points in order to make segments
if (count < 2) {
return;
}
// Create the touch (there should only be one touch object for the whole drag)
UITouch *touch = [[UITouch alloc] initAtPoint:points[0] inView:self];
[touch setPhase:UITouchPhaseBegan];
UIEvent *eventDown = [self _eventWithTouch:touch];
[[UIApplication sharedApplication] sendEvent:eventDown];
CFRunLoopRunInMode(UIApplicationCurrentRunMode, DRAG_TOUCH_DELAY, false);
for (NSInteger pointIndex = 1; pointIndex < count; pointIndex++) {
[touch setLocationInWindow:[self.window convertPoint:points[pointIndex] fromView:self]];
[touch setPhase:UITouchPhaseMoved];
UIEvent *eventDrag = [self _eventWithTouch:touch];
[[UIApplication sharedApplication] sendEvent:eventDrag];
CFRunLoopRunInMode(UIApplicationCurrentRunMode, DRAG_TOUCH_DELAY, false);
}
[touch setPhase:UITouchPhaseEnded];
UIEvent *eventUp = [self _eventWithTouch:touch];
[[UIApplication sharedApplication] sendEvent:eventUp];
// Dispatching the event doesn't actually update the first responder, so fake it
if (touch.view == self && [self canBecomeFirstResponder]) {
[self becomeFirstResponder];
}
while (UIApplicationCurrentRunMode != kCFRunLoopDefaultMode) {
CFRunLoopRunInMode(UIApplicationCurrentRunMode, 0.1, false);
}
[touch release];
}
- (BOOL)isProbablyTappable
{
// There are some issues with the tappability check in UIWebViews, so if the view is a UIWebView we will just skip the check.
return [NSStringFromClass([self class]) isEqualToString:@"UIWebBrowserView"] || self.isTappable;
}
// Is this view currently on screen?
- (BOOL)isTappable;
{
return [self isTappableInRect:self.bounds];
}
- (BOOL)isTappableInRect:(CGRect)rect;
{
CGPoint tappablePoint = [self tappablePointInRect:rect];
return !isnan(tappablePoint.x);
}
- (BOOL)isTappableWithHitTestResultView:(UIView *)hitView;
{
// Special case for UIControls, which may have subviews which don't respond to -hitTest:,
// but which are tappable. In this case the hit view will be the containing
// UIControl, and it will forward the tap to the appropriate subview.
// This applies with UISegmentedControl which contains UISegment views (a private UIView
// representing a single segment).
if ([hitView isKindOfClass:[UIControl class]] && [self isDescendantOfView:hitView]) {
return YES;
}
// Button views in the nav bar (a private class derived from UINavigationItemView), do not return
// themselves in a -hitTest:. Instead they return the nav bar.
if ([hitView isKindOfClass:[UINavigationBar class]] && [self isKindOfClass:NSClassFromString(@"UINavigationItemView")] && [self isDescendantOfView:hitView]) {
return YES;
}
return [hitView isDescendantOfView:self];
}
- (CGPoint)tappablePointInRect:(CGRect)rect;
{
// Start at the top and recurse down
CGRect frame = [self.window convertRect:rect fromView:self];
UIView *hitView = nil;
CGPoint tapPoint = CGPointZero;
// Mid point
tapPoint = CGPointCenteredInRect(frame);
hitView = [self.window hitTest:tapPoint withEvent:nil];
if ([self isTappableWithHitTestResultView:hitView]) {
return [self.window convertPoint:tapPoint toView:self];
}
// Top left
tapPoint = CGPointMake(frame.origin.x + 1.0f, frame.origin.y + 1.0f);
hitView = [self.window hitTest:tapPoint withEvent:nil];
if ([self isTappableWithHitTestResultView:hitView]) {
return [self.window convertPoint:tapPoint toView:self];
}
// Top right
tapPoint = CGPointMake(frame.origin.x + 1.0f + frame.size.width - 1.0f, frame.origin.y + 1.0f);
hitView = [self.window hitTest:tapPoint withEvent:nil];
if ([self isTappableWithHitTestResultView:hitView]) {
return [self.window convertPoint:tapPoint toView:self];
}
// Bottom left
tapPoint = CGPointMake(frame.origin.x + 1.0f, frame.origin.y + frame.size.height - 1.0f);
hitView = [self.window hitTest:tapPoint withEvent:nil];
if ([self isTappableWithHitTestResultView:hitView]) {
return [self.window convertPoint:tapPoint toView:self];
}
// Bottom right
tapPoint = CGPointMake(frame.origin.x + frame.size.width - 1.0f, frame.origin.y + frame.size.height - 1.0f);
hitView = [self.window hitTest:tapPoint withEvent:nil];
if ([self isTappableWithHitTestResultView:hitView]) {
return [self.window convertPoint:tapPoint toView:self];
}
return CGPointMake(NAN, NAN);
}
- (UIEvent *)_eventWithTouch:(UITouch *)touch;
{
UIEvent *event = [[UIApplication sharedApplication] performSelector:@selector(_touchesEvent)];
CGPoint location = [touch locationInView:touch.window];
KIFEventProxy *eventProxy = [[KIFEventProxy alloc] init];
eventProxy->x1 = location.x;
eventProxy->y1 = location.y;
eventProxy->x2 = location.x;
eventProxy->y2 = location.y;
eventProxy->x3 = location.x;
eventProxy->y3 = location.y;
eventProxy->sizeX = 1.0;
eventProxy->sizeY = 1.0;
eventProxy->flags = ([touch phase] == UITouchPhaseEnded) ? 0x1010180 : 0x3010180;
eventProxy->type = 3001;
NSSet *allTouches = [event allTouches];
[event _clearTouches];
[allTouches makeObjectsPerformSelector:@selector(autorelease)];
[event _setGSEvent:(struct __GSEvent *)eventProxy];
[event _addTouch:touch forDelayedDelivery:NO];
[eventProxy release];
return event;
}
- (BOOL)isUserInteractionActuallyEnabled;
{
BOOL isUserInteractionEnabled = self.userInteractionEnabled;
// Navigation item views don't have user interaction enabled, but their parent nav bar does and will forward the event
if (!isUserInteractionEnabled && [self isKindOfClass:NSClassFromString(@"UINavigationItemView")]) {
// If this view is inside a nav bar, and the nav bar is enabled, then consider it enabled
UIView *navBar = [self superview];
while (navBar && ![navBar isKindOfClass:[UINavigationBar class]]) {
navBar = [navBar superview];
}
if (navBar && navBar.userInteractionEnabled) {
isUserInteractionEnabled = YES;
}
}
// UIActionsheet Buttons have UIButtonLabels with userInteractionEnabled=NO inside,
// grab the superview UINavigationButton instead.
if (!isUserInteractionEnabled && [self isKindOfClass:NSClassFromString(@"UIButtonLabel")]) {
UIView *button = [self superview];
while (button && ![button isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
button = [button superview];
}
if (button && button.userInteractionEnabled) {
isUserInteractionEnabled = YES;
}
}
return isUserInteractionEnabled;
}
@end