Skip to content

Instantly share code, notes, and snippets.

@KittenYang
Created April 8, 2016 02:41
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save KittenYang/c4ed872777777e8dacc601ee3385eef6 to your computer and use it in GitHub Desktop.
Save KittenYang/c4ed872777777e8dacc601ee3385eef6 to your computer and use it in GitHub Desktop.
一行代码实现点击区域的扩大
void Swizzle(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);
}
}
@implementation UIView (ExtendTouchRect)
+ (void)load {
Swizzle(self, @selector(pointInside:withEvent:), @selector(myPointInside:withEvent:));
}
- (BOOL)myPointInside:(CGPoint)point withEvent:(UIEvent *)event {
if (UIEdgeInsetsEqualToEdgeInsets(self.touchExtendInset, UIEdgeInsetsZero) || self.hidden ||
([self isKindOfClass:UIControl.class] && !((UIControl *)self).enabled)) {
return [self myPointInside:point withEvent:event]; // original implementation
}
CGRect hitFrame = UIEdgeInsetsInsetRect(self.bounds, self.touchExtendInset);
hitFrame.size.width = MAX(hitFrame.size.width, 0); // don't allow negative sizes
hitFrame.size.height = MAX(hitFrame.size.height, 0);
return CGRectContainsPoint(hitFrame, point);
}
static char touchExtendInsetKey;
- (void)setTouchExtendInset:(UIEdgeInsets)touchExtendInset {
objc_setAssociatedObject(self, &touchExtendInsetKey, [NSValue valueWithUIEdgeInsets:touchExtendInset],
OBJC_ASSOCIATION_RETAIN);
}
- (UIEdgeInsets)touchExtendInset {
return [objc_getAssociatedObject(self, &touchExtendInsetKey) UIEdgeInsetsValue];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment