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
网友评论