Skip to content

Instantly share code, notes, and snippets.

@iccir
Created November 5, 2016 01:39
Show Gist options
  • Save iccir/b3b3663f8d057ec80981a7ab18d41202 to your computer and use it in GitHub Desktop.
Save iccir/b3b3663f8d057ec80981a7ab18d41202 to your computer and use it in GitHub Desktop.
MTSDebugTouchEvents
/*
When MTSDebugTouchEvents() is called, -[UIApplication sendEvent:] will flash
the views which receive touch events.
*/
@implementation UIApplication (MTSDebug)
- (void) mts_sendEvent_highlightTouches:(NSArray *)touches
{
static NSMutableDictionary *sViewToWindowMap = nil;
if (!sViewToWindowMap) {
sViewToWindowMap = [[NSMutableDictionary alloc] init];
}
for (UITouch *touch in touches) {
UIView *view = [touch view];
if (view) {
NSValue *viewValue = [NSValue valueWithPointer:(__bridge void *)view];
CGRect frame = [view convertRect:[view bounds] toView:nil];
UIWindow *window = [sViewToWindowMap objectForKey:viewValue];
if (!window) {
window = [[UIWindow alloc] initWithFrame:frame];
[window setUserInteractionEnabled:NO];
[window setWindowLevel:UIWindowLevelStatusBar + 1 + [sViewToWindowMap count]];
[window setHidden:NO];
[window setExclusiveTouch:NO];
[sViewToWindowMap setObject:window forKey:viewValue];
[window setBackgroundColor:[UIColor colorWithRed:0 green:1 blue:0 alpha:0.33]];
}
[window setFrame:frame];
[window setAlpha:1.0];
[UIView animateWithDuration:0.5 animations:^{
[window setAlpha:0];
} completion:^(BOOL finished) {
if (finished) {
[window removeFromSuperview];
[sViewToWindowMap removeObjectForKey:viewValue];
}
}];
}
NSLog(@"%@", [touch view]);
}
}
- (void) mts_sendEvent:(UIEvent *)event
{
[self mts_sendEvent:event];
[self performSelector:@selector(mts_sendEvent_highlightTouches:) withObject:[event allTouches] afterDelay:0];
}
@end
void MTSDebugTouchEvents()
{
static BOOL didSwizzle = NO;
if (!didSwizzle) {
MTSSwizzleMethod([UIApplication class], @selector(sendEvent:), @selector(mts_sendEvent:));
didSwizzle = YES;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment