Skip to content

Instantly share code, notes, and snippets.

@iccir
Last active September 1, 2015 23:03
Show Gist options
  • Save iccir/f953ecb9283bc19635fa to your computer and use it in GitHub Desktop.
Save iccir/f953ecb9283bc19635fa to your computer and use it in GitHub Desktop.
IXDebugTouchEvents()
// IXDebugTouchEvents() will flash the view that receives a touch event
BOOL IXSwizzleMethod(Class cls, SEL selA, SEL selB)
{
Method methodA = class_getInstanceMethod(cls, selA);
if (!methodA) return NO;
Method methodB = class_getInstanceMethod(cls, selB);
if (!methodB) return NO;
class_addMethod(cls, selA, class_getMethodImplementation(cls, selA), method_getTypeEncoding(methodA));
class_addMethod(cls, selB, class_getMethodImplementation(cls, selB), method_getTypeEncoding(methodB));
method_exchangeImplementations(class_getInstanceMethod(cls, selA), class_getInstanceMethod(cls, selB));
return YES;
}
void IXDebugTouchEvents()
{
static BOOL didSwizzle = NO;
if (!didSwizzle) {
IXSwizzleMethod([UIApplication class], @selector(sendEvent:), @selector(ix_sendEvent:));
didSwizzle = YES;
}
}
@implementation UIApplication (IXDebug)
- (void) ix_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) ix_sendEvent:(UIEvent *)event
{
[self ix_sendEvent:event];
[self performSelector:@selector(ix_sendEvent_highlightTouches:) withObject:[event allTouches] afterDelay:0];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment