iOS按钮暴力点击的便捷解决方案二

首先只响应第一次点击事件
#define defaultInterval .5  //默认时间间隔
@interface UIButton (touch)
/**设置点击时间间隔*/
@property (nonatomic, assign) NSTimeInterval timeInterval;
/**
 *  用于设置单个按钮不需要被hook
 */
@property (nonatomic, assign) BOOL isIgnore;
@end

@interface UIButton()
/**bool 类型 YES 不允许点击   NO 允许点击   设置是否执行点UI方法*/
@property (nonatomic, assign) BOOL isIgnoreEvent;
@end
@implementation UIButton (touch)
+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL selA = @selector(sendAction:to:forEvent:);
        SEL selB = @selector(mySendAction:to:forEvent:);
        Method methodA =   class_getInstanceMethod(self,selA);
        Method methodB = class_getInstanceMethod(self, selB);
        //将 methodB的实现 添加到系统方法中 也就是说 将 methodA方法指针添加成 方法methodB的  返回值表示是否添加成功
        BOOL isAdd = class_addMethod(self, selA, method_getImplementation(methodB), method_getTypeEncoding(methodB));
        //添加成功了 说明 本类中不存在methodB 所以此时必须将方法b的实现指针换成方法A的,否则 b方法将没有实现。
        if (isAdd) {
            class_replaceMethod(self, selB, method_getImplementation(methodA), method_getTypeEncoding(methodA));
        }else{
            //添加失败了 说明本类中 有methodB的实现,此时只需要将 methodA和methodB的IMP互换一下即可。
            method_exchangeImplementations(methodA, methodB);
        }
    });
}
- (NSTimeInterval)timeInterval{
    return [objc_getAssociatedObject(self, _cmd) doubleValue];
}
- (void)setTimeInterval:(NSTimeInterval)timeInterval{
    objc_setAssociatedObject(self, @selector(timeInterval), @(timeInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
}
//当我们按钮点击事件 sendAction 时  将会执行  mySendAction
- (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    if (self.isIgnore) {
        //不需要被hook
        [self mySendAction:action to:target forEvent:event];
        return;
    }
    if ([NSStringFromClass(self.class) isEqualToString:@"UIButton"]) {
        self.timeInterval =self.timeInterval == 0 ?defaultInterval:self.timeInterval;
        if (self.isIgnoreEvent){
            return;
        }else if (self.timeInterval > 0){
            [self performSelector:@selector(resetState) withObject:nil afterDelay:self.timeInterval];
        }
    }
    //此处 methodA和methodB方法IMP互换了,实际上执行 sendAction;所以不会死循环
    self.isIgnoreEvent = YES;
    [self mySendAction:action to:target forEvent:event];
}
//runtime 动态绑定 属性
- (void)setIsIgnoreEvent:(BOOL)isIgnoreEvent{
    // 注意BOOL类型 需要用OBJC_ASSOCIATION_RETAIN_NONATOMIC 不要用错,否则set方法会赋值出错
    objc_setAssociatedObject(self, @selector(isIgnoreEvent), @(isIgnoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isIgnoreEvent{
    //_cmd == @select(isIgnore); 和set方法里一致
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}
- (void)setIsIgnore:(BOOL)isIgnore{
    // 注意BOOL类型 需要用OBJC_ASSOCIATION_RETAIN_NONATOMIC 不要用错,否则set方法会赋值出错
    objc_setAssociatedObject(self, @selector(isIgnore), @(isIgnore), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isIgnore{
    //_cmd == @select(isIgnore); 和set方法里一致
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}
- (void)resetState{
    [self setIsIgnoreEvent:NO];
}


原理是这样的,首先将分类中的自定义的事件的方法和UIControl的方法通过method_exchangeImplementations互换一下,之后,根据用户设定的延迟时间,延迟调用刷新按钮的状态的,在延迟的期间,设置按钮的属性是默认不可点击的,延迟时间到达后,更新按钮的为可点击的状态。

连续点击只响应最后一次事件,忽略中间事件
/**设置点击时间间隔*/
@property (nonatomic, assign) NSInteger delayInterval;
@property (nonatomic, strong) dispatch_source_t timer;
@property (nonatomic, assign) BOOL isNeedDelay;
@end

#import "UIButton+Delay.h"
#import 
#define defaultInteral  @"1"
@implementation UIButton (Delay)
- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    if (!self.isNeedDelay) {
        [super sendAction:action to:target forEvent:event];
        return;
    }
    self.delayInterval = [defaultInteral integerValue] ; //倒计时时间
    if (self.timer) {
       dispatch_source_cancel(self.timer);
        self.timer = nil;
    
    }
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    if (!self.timer) {
        self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    }
    dispatch_source_set_timer(self.timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(self.timer, ^{
    if(self.delayInterval==0){ //倒计时结束,关闭
        dispatch_source_cancel(self.timer);
        self.timer = nil;
        [super sendAction:action to:target forEvent:event];
        self.delayInterval = [defaultInteral integerValue];
    }else{
        [super sendAction:@selector(handleAction:) to:self forEvent:event];
        self.delayInterval--;
    }
});
      dispatch_resume(self.timer);

}
- (void)handleAction:(id)sender {
    
}
- (NSInteger)delayInterval{
    return [objc_getAssociatedObject(self, _cmd) integerValue];
}
- (void)setDelayInterval:(NSInteger)delayInterval{
    objc_setAssociatedObject(self, @selector(delayInterval), @(delayInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
}
- (dispatch_source_t)timer{
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setTimer:(dispatch_source_t)timer{
    objc_setAssociatedObject(self, @selector(timer),timer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
}
- (void)setIsNeedDelay:(BOOL)isNeedDelay{
    // 注意BOOL类型 需要用OBJC_ASSOCIATION_RETAIN_NONATOMIC 不要用错,否则set方法会赋值出错
    objc_setAssociatedObject(self, @selector(isNeedDelay), @(isNeedDelay), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isNeedDelay{
    //_cmd == @select(isIgnore); 和set方法里一致
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}
@end

主要用到定时器

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    if (!self.timer) {
        self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    }
    dispatch_source_set_timer(self.timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(self.timer, ^{
    if(self.delayInterval==0){ //倒计时结束,关闭
        dispatch_source_cancel(self.timer);
        self.timer = nil;
        [super sendAction:action to:target forEvent:event];
        self.delayInterval = [defaultInteral integerValue];
    }else{
        [super sendAction:@selector(handleAction:) to:self forEvent:event];
        self.delayInterval--;
    }
});
      dispatch_resume(self.timer);

http://www.jianshu.com/p/8e562c24039e

定时器结束的时候,在调用父类的 [super sendAction:action to:target forEvent:event];响应点击。

你可能感兴趣的:(iOS按钮暴力点击的便捷解决方案二)