iOS防重点击

1.iOS按钮防重点击

创建UIButton的分类,需要注意的是,load方法里,互换系统方法时的处理,直接执行method_exchangeImplementations,点击tabbar按钮会crash,提示找不到对应方法;

.h文件
@interface UIButton (FixMultiClick)

/**
点击间隔:默认0.5s,对所有按钮生效:设置0s无效,如果不需要放多点处理,需要设置cl_noFix=YES;
针对打电话,可设置1s左右
*/
@property (nonatomic, assign) NSTimeInterval cl_acceptEventInterval;

/**
不做防多点的处理:如有些按钮【点击完】需要【立马执行】sendActionsForControlEvents:方法,此时如果操作了防多点,则改方法会被阻断;
解决方法是:设置cl_noFix=YES;
并不是所有执行sendActionsForControlEvents:方法的,都需要设置cl_noFix=YES;非立马执行的,就不存在此问题
默认为NO
*/
@property (nonatomic, assign) BOOL cl_noFix;

@end

.m文件

import "UIButton+FixMultiClick.h"

import

define defaultInterval 0.5

static const char *UIButton_acceptEventInterval = "UIButton_acceptEventInterval";
static const char *UIButton_ignoreEvent = "UIButton_ignoreEvent";
static const char *UIButton_noFix = "UIButton_noFix";

@interface UIButton ()

/**
太短时间间隔内(默认是0.5s),忽略新的点击事件:默认为No
*/
@property (nonatomic, assign) BOOL cl_ignoreEvent;

@end

@implementation UIButton (FixMultiClick)

/**
顾名思义,load方法在这个文件被程序装载时调用。只要是在Compile Sources中出现的文件总是会被装载,这与这个类是否被用到无关,因此load方法总是在main函数之前调用。
*/

  • (void)load
    {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    //点击事件
    SEL selA = @selector(sendAction:to:forEvent:);
    SEL selB = @selector(fixMultiClickSendAction: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));
    if (isAdd) {
    //添加成功,说明本类中不存在methodB,所以此时必须将methodB的实现指针换成methodA的,否则methodB将没有实现
    class_replaceMethod(self, selB, method_getImplementation(methodA), method_getTypeEncoding(methodA));
    } else {
    //添加失败,说明本类中methodB的实现,只需将methodA和methodB的IMP互换即可
    method_exchangeImplementations(methodA, methodB);
    }
    });
    }
  • (void)fixMultiClickSendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
    {
    if (self.cl_noFix) {
    //不做防多点的处理,如有些按钮需要执行sendActionsForControlEvents:方法
    [self fixMultiClickSendAction:action to:target forEvent:event];
    return;
    }
    if (self.cl_ignoreEvent) {
    return;
    }
    //将cl_ignoreEvent的设置提前,防止在这期间来了新的点击事件,从而没有达到阻断的目的
    self.cl_ignoreEvent = YES;
    if ([NSStringFromClass(self.class) isEqualToString:@"UIButton"]) {
    //cl_acceptEventInterval为0时,设置为默认0.5s
    if (!self.cl_acceptEventInterval) {
    self.cl_acceptEventInterval = defaultInterval;
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.cl_acceptEventInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //指定间隔后,放开阻断
    self.cl_ignoreEvent = NO;
    });
    } else {
    //非UIButton(如UITabButton),不做此处理
    self.cl_ignoreEvent = NO;
    }
    [self fixMultiClickSendAction:action to:target forEvent:event];
    }

pragma mark - getter/setter

  • (NSTimeInterval)cl_acceptEventInterval
    {
    return [objc_getAssociatedObject(self, UIButton_acceptEventInterval) doubleValue];
    }

  • (void)setCl_acceptEventInterval:(NSTimeInterval)cl_acceptEventInterval
    {
    objc_setAssociatedObject(self, UIButton_acceptEventInterval, @(cl_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

  • (BOOL)cl_ignoreEvent
    {
    return [objc_getAssociatedObject(self, UIButton_ignoreEvent) boolValue];
    }

  • (void)setCl_ignoreEvent:(BOOL)cl_ignoreEvent
    {
    objc_setAssociatedObject(self, UIButton_ignoreEvent, @(cl_ignoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

  • (BOOL)cl_noFix
    {
    return [objc_getAssociatedObject(self, UIButton_noFix) boolValue];
    }

  • (void)setCl_noFix:(BOOL)cl_noFix
    {
    objc_setAssociatedObject(self, UIButton_noFix, @(cl_noFix), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

@end

2.对cell添加防重处理

首先,贴方法

  • (BOOL)fastSelectedCell
    {
    if (self.ignoreEvent) {
    return YES;
    }
    self.ignoreEvent = YES;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.acceptEventInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    self.ignoreEvent = NO;
    });
    return NO;
    }
    在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法中,判断是否快速点击,再执行操作。

你可能感兴趣的:(iOS防重点击)