Skip to content

Instantly share code, notes, and snippets.

@soulfly
Last active June 3, 2016 09:29
Show Gist options
  • Save soulfly/9e53eae3cd4a20993e36146cd3d49ecc to your computer and use it in GitHub Desktop.
Save soulfly/9e53eae3cd4a20993e36146cd3d49ecc to your computer and use it in GitHub Desktop.
This UIControl extension blocks frequent touches
// Created at: Thursday, February 16, 2012 at 4:29 PM
// replace method util
static void MethodSwizzle(Class c, SEL orig, SEL new){
Method origMethod = class_getInstanceMethod(c, orig);
Method newMethod = class_getInstanceMethod(c, new);
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))){
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
} else {
method_exchangeImplementations(origMethod, newMethod);
}
}
...
// replace methods
MethodSwizzle([UIControl class],
@selector(beginTrackingWithTouch:withEvent:),
@selector(customBeginTrackingWithTouch:withEvent:)
);
MethodSwizzle([UIControl class],
@selector(endTrackingWithTouch:withEvent:),
@selector(customEndTrackingWithTouch:withEvent:)
);
MethodSwizzle([UIControl class],
@selector(cancelTrackingWithEvent:),
@selector(customCancelTrackingWithEvent:)
);
...
#define minIntervalBetweenUIControlActivity 0.5
static NSTimeInterval lastUIControlActivityTime = 0;
@implementation UIControl (Additional)
- (BOOL)customBeginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
//DLog(@"customBeginTrackingWithTouch");
double interval = CFAbsoluteTimeGetCurrent() - lastUIControlActivityTime;
if (interval > minIntervalBetweenUIControlActivity) {
if(!self.isFirstResponder){// fix for textField (Dont disable userInteraction when keyboard is showed)
//[((AppDelegate_Pad *)[[UIApplication sharedApplication] delegate]).window setUserInteractionEnabled:NO];
}
lastUIControlActivityTime = CFAbsoluteTimeGetCurrent();
BOOL value = [self customBeginTrackingWithTouch:touch withEvent:event];
// DLog(@"customBeginTrackingWithTouch bool %d", value);
return value;
}
// DLog(@"UIControl not allow");
return NO;
}
- (void)customEndTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
// DLog(@"customEndTrackingWithTouch");
//[((AppDelegate_Pad *)[[UIApplication sharedApplication] delegate]).window setUserInteractionEnabled:YES];
[self customEndTrackingWithTouch:touch withEvent:event];
}
- (void)customCancelTrackingWithEvent:(UIEvent *)event{
// DLog(@"customCancelTrackingWithEvent");
//[((AppDelegate_Pad *)[[UIApplication sharedApplication] delegate]).window setUserInteractionEnabled:YES];
[self customCancelTrackingWithEvent:event];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment