如下代码:创建一个UIControl的分类UIControl (Extension)
#import<UIKit/UIkit.h>
#define NAVIITEM_INTERVAL 0.5
@interface UIControl (Extension)
//两次响应之间的时间间隔
@property (nonatomic, assign) NSTimeInterval uxy_acceptEventInterval;
//是否忽略事件true 忽略 flase不忽略
@property (nonatomic, strong) NSNumber *uxy_ignoreEvent;
@end
#import "UIControl+Extension.h"
@implementation UIControl (Extension)
static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";
static const char *uxy_ignoreEventKey = "uxy_ignoreEventKey";
- (NSTimeInterval)uxy_acceptEventInterval
{
return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
}
- (void)setUxy_acceptEventInterval:(NSTimeInterval)uxy_acceptEventInterval
{
objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(uxy_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSNumber *)uxy_ignoreEvent{
return objc_getAssociatedObject(self, uxy_ignoreEventKey);
}
-(void)setUxy_ignoreEvent:(NSNumber *)uxy_ignoreEvent{
objc_setAssociatedObject(self, uxy_ignoreEventKey, uxy_ignoreEvent, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+ (void)load
{
Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
Method b = class_getInstanceMethod(self, @selector(__uxy_sendAction:to:forEvent:));
method_exchangeImplementations(a, b);
}
- (void)__uxy_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
if (self.uxy_ignoreEvent.boolValue) return;
if (self.uxy_acceptEventInterval > 0)
{
self.uxy_ignoreEvent = @(YES);
[self performSelector:@selector(setUxy_ignoreEvent:) withObject:@(NO) afterDelay:self.uxy_acceptEventInterval];
}
[self __uxy_sendAction:action to:target forEvent:event];
}
@end
在创建button的地方设置2次点击的间隔时间就可以了。如:
UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.uxy_acceptEventInterval = NAVIITEM_INTERVAL;
网友评论