Created
March 12, 2016 03:40
-
-
Save phuna/d5c72168be668f200f16 to your computer and use it in GitHub Desktop.
IOS - Touch alert for all UIViews to help finding which part of the screen user touched
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import UIKit; | |
@interface UIView (TouchAlert) | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Base on Marc's article here: | |
// http://darkdust.net/writings/objective-c/method-swizzling | |
#import "UIView+TouchAlert.h" | |
#import <objc/runtime.h> | |
@implementation UIView (TouchAlert) | |
- (id)swizzled_initWithFrame:(CGRect)frame | |
{ | |
id result = [self swizzled_initWithFrame:frame]; | |
// Safe guard: do we have an UIView (or something that has a layer)? | |
if ([result respondsToSelector:@selector(layer)]) { | |
UITapGestureRecognizer *allViewTouch = | |
[[UITapGestureRecognizer alloc] initWithTarget:self | |
action:@selector(allTouchsAlert:)]; | |
allViewTouch.cancelsTouchesInView = NO; | |
[self addGestureRecognizer:allViewTouch]; | |
} | |
// Return the modified view. | |
return result; | |
} | |
- (id)swizzled_initWithCoder:(NSCoder *)aDecoder | |
{ | |
id result = [self swizzled_initWithCoder:aDecoder]; | |
// Safe guard: do we have an UIView (or something that has a layer)? | |
if ([result respondsToSelector:@selector(layer)]) { | |
UITapGestureRecognizer *allViewTouch = | |
[[UITapGestureRecognizer alloc] initWithTarget:self | |
action:@selector(allTouchsAlert:)]; | |
allViewTouch.cancelsTouchesInView = NO; | |
[self addGestureRecognizer:allViewTouch]; | |
} | |
// Return the modified view. | |
return result; | |
} | |
- (void)allTouchsAlert:(UITapGestureRecognizer *)recognizer { | |
NSLog(@"Touch in view: %@", NSStringFromClass([self class])); | |
for (UIView* next = self; next; next = next.superview) | |
{ | |
UIResponder* nextResponder = [next nextResponder]; | |
if ([nextResponder isKindOfClass:[UIViewController class]]) | |
{ | |
NSLog(@"Parent VC: %@", nextResponder); | |
break; | |
} | |
} | |
} | |
+ (void)load | |
{ | |
// The "+ load" method is called once, very early in the application life-cycle. | |
// It's called even before the "main" function is called. Beware: there's no | |
// autorelease pool at this point, so avoid Objective-C calls. | |
Method original, swizzle; | |
// Get the "- (id)initWithFrame:" method. | |
original = class_getInstanceMethod(self, @selector(initWithFrame:)); | |
// Get the "- (id)swizzled_initWithFrame:" method. | |
swizzle = class_getInstanceMethod(self, @selector(swizzled_initWithFrame:)); | |
// Swap their implementations. | |
method_exchangeImplementations(original, swizzle); | |
// Get the "- (id)initWithCoder:" method. | |
original = class_getInstanceMethod(self, @selector(initWithCoder:)); | |
// Get the "- (id)swizzled_initWithCoder:" method. | |
swizzle = class_getInstanceMethod(self, @selector(swizzled_initWithCoder:)); | |
// Swap their implementations. | |
method_exchangeImplementations(original, swizzle); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment