1、button點擊事件監測
通過runtime獲取button的點擊事件,進行全局監測
#import "UIControl+XHControl.h"
#import <objc/runtime.h>
@implementation UIControl (XHControl)
+ (void)load
{
Method m1 = class_getInstanceMethod([self class], @selector(sendAction:to:forEvent:));
Method m2 = class_getInstanceMethod([self class], @selector(xh_sendAction:to:forEvent:));
BOOL addSuccess = class_addMethod([self class], @selector(sendAction:to:forEvent:), method_getImplementation(m2), method_getTypeEncoding(m2));
if (!addSuccess) {
method_exchangeImplementations(m1, m2);
}
}
- (void)xh_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
[self xh_sendAction:action to:target forEvent:event];
}
2、button多次點擊
-
設置button.enable屬性進行管理
-
通過runtime,監控點擊方法,添加時間屬性進行管理
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIControl (XHControl)
/***/
@property (nonatomic,strong) NSDate *currentDate;
@end
NS_ASSUME_NONNULL_END
#import "UIControl+XHControl.h"
#import <objc/runtime.h>
NSTimeInterval const delayInterval = 1;
static const char *delayConst = 0;
@implementation UIControl (XHControl)
- (void)setCurrentDate:(NSDate *)currentDate
{
objc_setAssociatedObject(self, &delayConst, currentDate, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSDate *)currentDate{
return objc_getAssociatedObject(self, &delayConst);
}
+ (void)load
{
Method m1 = class_getInstanceMethod([self class], @selector(sendAction:to:forEvent:));
Method m2 = class_getInstanceMethod([self class], @selector(xh_sendAction:to:forEvent:));
BOOL addSuccess = class_addMethod([self class], @selector(xh_sendAction:to:forEvent:), method_getImplementation(m2), method_getTypeEncoding(m2));
if (addSuccess) {
method_exchangeImplementations(m1, m2);
}
}
- (void)xh_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
if ([[NSDate date] timeIntervalSinceDate: self.currentDate] <= delayInterval) {
//這裏的delayInterval也可以通過添加一個屬性變量進行傳值
return;
}
self.currentDate = [NSDate date];
NSLog(@"234");
[self xh_sendAction:action to:target forEvent:event];
}
@end