Skip to content

Instantly share code, notes, and snippets.

@phuna
Created March 12, 2016 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phuna/d5c72168be668f200f16 to your computer and use it in GitHub Desktop.
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
@import UIKit;
@interface UIView (TouchAlert)
@end
// 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