iOS程序猿程序员iOS开发攻城狮的集散地

iOS 防止UIButton暴力点击

2018-09-14  本文已影响45人  骑马纵天下
- (void)buttonClick:(UIButton *)violenceBtn{
    //点击按钮后先取消之前的操作,再进行需要进行的操作
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleClickEvent:) object:violenceBtn];
    [self performSelector:@selector(handleClickEvent:) withObject:violenceBtn afterDelay:0.5f];//时间0.5
}
//点击事件的处理方法
- (void)handleClickEvent:(UIButton *)btn{
    
    NSLog(@" %s ",__FUNCTION__);
}
-(void)buttonClick:(UIButton*)violenceBtn{
    violenceBtn.enabled =NO;
    NSLog(@" %s ",__FUNCTION__);
    [self performSelector:@selector(changeButtonStatus) withObject:nil afterDelay:1.0f];//防止重复点击
}
-(void)changeButtonStatus{
    _btn.enabled =YES;
}
-(void)buttonClick:(UIButton*)violenceBtn{
    static NSTimeInterval time = 0.0;
    NSTimeInterval currentTime = [NSDate date].timeIntervalSince1970;//防止暴力点击 两秒内只能点击一次
    if (currentTime - time > 2) {//限制用户点击按钮的时间间隔大于2秒钟
        NSLog(@" %s ",__FUNCTION__);
    }
    time = currentTime;

}
/**
 防止按钮重复暴力点击
 */
@interface UIButton (ViolenceBtn)
//点击间隔
@property (nonatomic, assign) NSTimeInterval timeInterval;
//用于设置单个按钮不需要被hook (设为yes说明可以重复点击)
@property (nonatomic, assign) BOOL isIgnore;
@end



#import "UIButton+ViolenceBtn.h"
#import <objc/runtime.h>

//默认时间间隔
#define defaultInterval 1
@implementation UIButton (ViolenceBtn)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        SEL orginalSel = @selector(sendAction:to:forEvent:);
        SEL swizzledSel = @selector(sure_SendAction:to:forEvent:);
        //原有方法
        Method originalMethod = class_getInstanceMethod(class, orginalSel);
        //替换原有方法的新方法
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSel);
        //先尝试給源SEL添加IMP,这里是为了避免源SEL没有实现IMP的情况
        //将methodB的实现添加到系统方法中也就是说将methodA方法指针添加成方法methodB的返回值表示是否添加成功
        BOOL didAddMethod = class_addMethod(class, orginalSel,
                                            method_getImplementation(swizzledMethod),
                                            method_getTypeEncoding(swizzledMethod));
        //添加成功了说明本类中不存在methodB所以此时必须将方法b的实现指针换成方法A的,否则b方法将没有实现。
        if (didAddMethod) { //添加成功:说明源SEL没有实现IMP,将源SEL的IMP替换到交换SEL的IMP
            class_replaceMethod(class, swizzledSel,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else { //添加失败:说明源SEL已经有IMP,直接将两个SEL的IMP交换即可
            //添加失败了说明本类中有methodB的实现,此时只需要将methodA和methodB的IMP互换一下即可。
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

- (NSTimeInterval)timeInterval {
    return [objc_getAssociatedObject(self, _cmd) doubleValue];
}
- (void)setTimeInterval:(NSTimeInterval)timeInterval {
    objc_setAssociatedObject(self, @selector(timeInterval), @(timeInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
//当按钮点击事件sendAction 时将会执行sure_SendAction
- (void)sure_SendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    if (self.isIgnore) {
        //不需要被hook
        [self sure_SendAction: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 sure_SendAction: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];
}
@end
时间间隔

目前来说通过runtime最方便,但是如果是不同按钮同时点击情况还会发生,所以可以使用最合适的方法。

上一篇下一篇

猜你喜欢

热点阅读