Last active
August 29, 2015 14:00
-
-
Save hetima/465b897da0f03e9288b9 to your computer and use it in GitHub Desktop.
KZRMETHOD_SWIZZLING_WITHBLOCK
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
#define KZRMETHOD_SWIZZLING_WITHBLOCK(className, selectorName, isClassMethod, originalIMP, originalSelector, block) {\ | |
BOOL _val_isClassMethod=isClassMethod; \ | |
const char* _val_selName=selectorName; \ | |
if(*_val_selName=='+'){_val_isClassMethod=YES; _val_selName++;} \ | |
Class _val_cls=objc_getClass(className); \ | |
SEL originalSelector=sel_registerName(_val_selName); \ | |
Method _val_originalMethod; \ | |
if (_val_isClassMethod)_val_originalMethod = class_getClassMethod(_val_cls, originalSelector); \ | |
else _val_originalMethod = class_getInstanceMethod(_val_cls, originalSelector); \ | |
KZRIMPUnion originalIMP = (KZRIMPUnion)(IMP)method_getImplementation(_val_originalMethod); \ | |
if (originalIMP.as_id) { id _val_block=block \ | |
;IMP _val_newImp = imp_implementationWithBlock(_val_block); \ | |
method_setImplementation(_val_originalMethod, _val_newImp); \ | |
} \ | |
} | |
KZRMETHOD_SWIZZLING_WITHBLOCK( | |
"NSView", | |
"frame", | |
KZRInstanceMethod, originalIMP, originalSelector, | |
^NSRect (id rself){ // SEL is not brought (id self, arg1, arg2...) | |
NSRect result=originalIMP.as_rect(rself, originalSelector); | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
NSLog(@"frame=%@", NSStringFromRect(result)); | |
}); | |
return result; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment