美文网首页
hook方法

hook方法

作者: Smallwolf_JS | 来源:发表于2018-11-07 15:44 被阅读15次

直接上代码

//  UITableView+ext.m
//  VideoIphone
//
//  Created by baidu on 2018/11/7.
//  Copyright © 2018 com.baidu. All rights reserved.
//

#import "UITableView+ext.h"
#import <objc/message.h>
@implementation UITableView (ext)



+ (void)load{
    Class class = [self class];
    SEL originalSelector = @selector(setContentOffset:animated:);
    SEL swizzledSelector = @selector(ext_setContentOffset:animated:);

    Method originalMethod = class_getInstanceMethod(class, @selector(setContentOffset:animated:));
    Method swizzledMethod = class_getInstanceMethod(class, @selector(ext_setContentOffset:animated:));
    BOOL success = class_addMethod([self class],
                                   originalSelector,
                                   method_getImplementation(swizzledMethod),
                                   method_getTypeEncoding(swizzledMethod));
    if (success) {
        class_replaceMethod([self class],
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }


}
- (void)ext_setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated{
    NSLog(@" hook到的偏移量:******%f",contentOffset.y);
    [self ext_setContentOffset:contentOffset animated:animated];
}

@end

相关文章

网友评论

      本文标题:hook方法

      本文链接:https://www.haomeiwen.com/subject/wfyvxqtx.html