美文网首页
UIButton hook 所有按钮点击事件

UIButton hook 所有按钮点击事件

作者: 阳光下的灰尘 | 来源:发表于2021-08-24 13:46 被阅读0次

直接上代码

//
//  UIButton+WLClickAction.m
//  Wallet
//
//  Created by HaiJun on 2021/8/24.
//  Copyright © 2021 SwiftPass. All rights reserved.
//

#import "UIButton+WLClickAction.h"
#import <objc/runtime.h>

@implementation UIButton (WLClickAction)

+ (void)load{

    [super load];

//拿到sendAction方法,
    Method oldObjectAtIndex =class_getInstanceMethod([UIButton class],@selector(sendAction:to:forEvent:));

//定义一个新的方法custom_sendAction
    Method newObjectAtIndex =class_getInstanceMethod([UIButton class], @selector(custom_sendAction:to:forEvent:));

//交换两个方法的指针
    method_exchangeImplementations(oldObjectAtIndex, newObjectAtIndex);

}

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    [super sendAction:action to:target forEvent:event];
}

- (void)custom_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    [self custom_sendAction:action to:target forEvent:event];
    NSLog(@"自定义方法---custom_sendAction");
//    if (1==1) {
//        NSLog(@"自定义方法");
//    }else{
////调用custom_sendAction方法,其实指针是指向的原来的sendAction方法
//    }
}

@end

相关文章

网友评论

      本文标题:UIButton hook 所有按钮点击事件

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