Created
August 20, 2018 11:59
-
-
Save manajay/350edd924e274e945c491e6b623dc735 to your computer and use it in GitHub Desktop.
method-swizzling-oc
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
+ (void)ls_methodSwizzlingWithOriginalSelector:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector { | |
Class class = [self class]; | |
Method originalMethod = class_getInstanceMethod(class, originalSelector); | |
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); | |
// When swizzling a class method, use the following: | |
// Class class = object_getClass((id)self); | |
// ... | |
// Method originalMethod = class_getClassMethod(class, originalSelector); | |
// Method swizzledMethod = class_getClassMethod(class, swizzledSelector); | |
BOOL didAddMethod = | |
class_addMethod(class, | |
originalSelector, | |
method_getImplementation(swizzledMethod), | |
method_getTypeEncoding(swizzledMethod)); | |
if (didAddMethod) { | |
class_replaceMethod(class, | |
swizzledSelector, | |
method_getImplementation(originalMethod), | |
method_getTypeEncoding(originalMethod)); | |
} else { | |
method_exchangeImplementations(originalMethod, swizzledMethod); | |
} | |
} | |
+ (void)load { | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
[self ls_methodSwizzlingWithOriginalSelector:@selector(scrollViewDidScroll:) swizzledSelector:@selector(ls_scrollViewDidScroll:)]; | |
[self ls_methodSwizzlingWithOriginalSelector:@selector(scrollViewWillBeginDragging:) swizzledSelector:@selector(ls_scrollViewWillBeginDragging:)]; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment