Open bestswifter opened 7 years ago
Nice! Thx for the post. I ended up with the following category based on your approach.
static IMP __original_TouchesBegan_Method_Imp;
@implementation UIView (Touch)
+ (void)load {
Method touchesBegan = class_getInstanceMethod([UIView class], @selector(touchesBegan:withEvent:));
Method xxx_touchesBegan = class_getInstanceMethod([UIView class], @selector(xxx_touchesBegan:withEvent:));
__original_TouchesBegan_Method_Imp = method_getImplementation(touchesBegan);
method_exchangeImplementations(touchesBegan, xxx_touchesBegan);
}
- (void)xxx_touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
void (*functionPointer)(id, SEL, NSSet<UITouch *> *, UIEvent *) = (void (*)(id, SEL, NSSet<UITouch *> *, UIEvent *))__original_TouchesBegan_Method_Imp;
functionPointer(self, _cmd, touches, event);
// Your logic here
}
@end
Inspired from this blog: The Right Way to Swizzle in Objective-C
I met the same problem when trying to swizzle
touchesBegan
method ofUIView
. My solution is using IMP directly.