[iOS ]Runtime防止按钮重复点击

[iOS ]Runtime防止按钮重复点击

image.png

在UIButton+Delay.m文件中增加属性

// 重复点击间隔
@property (nonatomic, assign) NSTimeInterval acceptEventInterval;
// 上一次点击时间戳
@property (nonatomic, assign) NSTimeInterval acceptEventTime;

重写load方法,利用Runtime进行方法交换

 + (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];

        SEL originalSelector = @selector(sendAction:to:forEvent:);
        SEL swizzledSelector = @selector(mySendAction:to:forEvent:);

        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

        BOOL didAddMethod = class_addMethod(class,
                                            originalSelector,
                                            method_getImplementation(swizzledMethod),
                                            method_getTypeEncoding(swizzledMethod));

        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

实现要交换的方法判断是否短时间内连续点击

- (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    
    // 如果想要设置统一的间隔时间,可以在此处加上以下几句
    if (self.acceptEventInterval <= 0) {
        // 如果没有自定义时间间隔,则默认为 0.4 秒
        self.acceptEventInterval = 0.4;
    }
    
    // 是否小于设定的时间间隔
    BOOL needSendAction = (NSDate.date.timeIntervalSince1970 - self.acceptEventTime >= self.acceptEventInterval);
    
    // 更新上一次点击时间戳
    if (self.acceptEventInterval > 0) {
        self.acceptEventTime = NSDate.date.timeIntervalSince1970;
    }
    
    // 两次点击的时间间隔小于设定的时间间隔时,才执行响应事件
    if (needSendAction) {
        [self mySendAction:action to:target forEvent:event];
    }else{
        NSLog(@"连续点击,不响应");
    }
}

利用Runtime动态添加属性


- (NSTimeInterval )acceptEventInterval{
    return [objc_getAssociatedObject(self, "UIControl_acceptEventInterval") doubleValue];
}

- (void)setAcceptEventInterval:(NSTimeInterval)acceptEventInterval{
    objc_setAssociatedObject(self, "UIControl_acceptEventInterval", @(acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSTimeInterval )acceptEventTime{
    return [objc_getAssociatedObject(self, "UIControl_acceptEventTime") doubleValue];
}

- (void)setAcceptEventTime:(NSTimeInterval)acceptEventTime{
    objc_setAssociatedObject(self, "UIControl_acceptEventTime", @(acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

补充:最近被QA提了一个bug,调用系统摄像头拍照的时候点击拍摄按钮没有反应,经排查,是因为添加了UIButton+Delay.h类。解决方法如下:

- (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    
    // 如果想要设置统一的间隔时间,可以在此处加上以下几句
    if (self.acceptEventInterval <= 0) {
        // 如果没有自定义时间间隔,则默认为 0.4 秒
        self.acceptEventInterval = 0.4;
    }
    
    // 是否小于设定的时间间隔
    BOOL needSendAction = (NSDate.date.timeIntervalSince1970 - self.acceptEventTime >= self.acceptEventInterval);
    
    // 更新上一次点击时间戳
    if (self.acceptEventInterval > 0) {
        self.acceptEventTime = NSDate.date.timeIntervalSince1970;
    }
    NSString *method = NSStringFromSelector(action);
    if ([method isEqualToString:@"_handleShutterButtonPressed:"]) {
        //点击相机拍照按钮事件则不判断是连续点击.
        [self mySendAction:action to:target forEvent:event];
        return;
    }
    // 两次点击的时间间隔小于设定的时间间隔时,才执行响应事件
    if (needSendAction) {
        [self mySendAction:action to:target forEvent:event];
    }else{
        NSLog(@"连续点击,不响应");
    }
}

文章均来自互联网如有不妥请联系作者删除QQ:314111741 地址:http://www.mqs.net/post/13796.html

相关阅读

  • 淘宝怎么运营推广(中小卖家必学的操作思路)

    淘宝怎么运营推广(中小卖家必学的操作思路)

    淘宝在很长一段时间内,一直被认为是中国最大的电子商务平台。人们在这里购物,与在别处购物一样,会感觉到很便捷、很实惠。因此,随着电商行业的发展。如今,淘宝网站的活跃用户数已经超过了1亿人(目前该数据仅统计了部分用户)。而作为一个淘宝...

    2025.12.09 14:15:37作者:iseeyuTags:运营
  • 如何保证缓存和数据的双写一致性

    如何保证缓存和数据的双写一致性

    image 但是在更新缓存方面,对于更新完数据库,是更新缓存呢,还是删除缓存。又或者是先删除缓存,再更新数据库,其实大家存在很大的争议。目前没有一篇全面的博客,对这几种方案进行解析。于是博主战战兢兢,顶着被大家喷的风险,写了这篇...

    2025.12.09 09:28:14作者:iseeyu
  • 【百度搜索引擎优化】如何快速了解百度搜索引擎优化的知识?(搜索引擎优化基本)

    【百度搜索引擎优化】如何快速了解百度搜索引擎优化的知识?(搜索引擎优化基本)

    在百度输入SEO优化,下拉框就有很多关键词,SEO优化工具,SEO查询,SEO技巧,SEO优化方案,SEO报价,SEO优化教程,SEO优化软件,SEO优化怎么做,等等,相关搜索也有很多长尾关键词。还可以加入一些群,找些大牛问下,向这些大牛学...

    2025.12.09 07:37:38作者:iseeyu

添加新评论