UIButton hook 所有按钮点击事件

2021-08-24  本文已影响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
上一篇 下一篇

猜你喜欢

热点阅读