UIButton点击间隔设置

场景

当app有点卡的时候,多次点击相同的button,经常出现,跳转了N次相同的界面

解决办法

用运行时和分类,替换UIControl响应事件,根据响应的间隔时间来判断是否执行事件。

另一种解决方案就是button点击事件执行时设置button的用户交互属性,再次只讨论设置两次点击间隔的问题。

详细步骤

创建一个UIControl的分类

![1278915-5087176d34bb4814.png](http://upload-images.jianshu.io/upload_images/2816873-4fde65e377667796.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

为了方便他人调整不同的间隔时间需求,在UIControl+Custom.h文件中开放间隔时间属性,UIControl+Custom.h文件的代码为:

<pre><code>

import <UIKit/UIKit.h>

@interface UIControl (Custom)

@property (nonatomic, assign) NSTimeInterval custom_acceptEventInterval;// 可以用这个给重复点击加间隔

@end
</pre></code>

UIControl+Custom.m文件中实现方法交换(妥善的做法是:先添加方法,如果方法已经存在,就替换原方法),在UIControl+Custom.m文件的代码为:

<pre><code>

import "UIControl+custom.h"

import <objc/runtime.h>

@interface UIControl()

@property (nonatomic, assign) NSTimeInterval custom_acceptEventTime;

@end

@implementation UIControl (Custom)

+ (void)load{

Method systemMethod = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
SEL sysSEL = @selector(sendAction:to:forEvent:);

  Method customMethod = class_getInstanceMethod(self, @selector(custom_sendAction:to:forEvent:));
  SEL customSEL = @selector(custom_sendAction:to:forEvent:);

  //添加方法 语法:BOOL class_addMethod(Class cls, SEL name, IM P imp, const char *types) 若添加成功则返回No
  // cls:被添加方法的类  name:被添加方法方法名  imp:被添加方法的实现函数  types:被添加方法的实现函数的返回值类型和参数类型的字符串
BOOL didAddMethod = class_addMethod(self, sysSEL, method_getImplementation(customMethod), method_getTypeEncoding(customMethod));

  //如果系统中该方法已经存在了,则替换系统的方法  语法:IMP class_replaceMethod(Class cls, SEL name, IMP imp,const char *types)
  if (didAddMethod) {
    class_replaceMethod(self, customSEL, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
  }else{
    method_exchangeImplementations(systemMethod, customMethod);
    
  }
  }

- (NSTimeInterval )custom_acceptEventInterval{
    return [objc_getAssociatedObject(self, "UIControl_acceptEventInterval") doubleValue];
  }
    
- (void)setCustom_acceptEventInterval:(NSTimeInterval)custom_acceptEventInterval{
    objc_setAssociatedObject(self, "UIControl_acceptEventInterval", @(custom_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  }
        
- (NSTimeInterval )custom_acceptEventTime{
    return [objc_getAssociatedObject(self, "UIControl_acceptEventTime") doubleValue];

}

- (void)setCustom_acceptEventTime:(NSTimeInterval)custom_acceptEventTime{
    objc_setAssociatedObject(self, "UIControl_acceptEventTime", @(custom_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  }
                
- (void)custom_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
                    
      // 如果想要设置统一的间隔时间,可以在此处加上以下几句
      // 值得提醒一下:如果这里设置了统一的时间间隔,会影响UISwitch,如果想统一设置,又不想影响UISwitch,建议将UIControl分类,改成UIButton分类,实现方法是一样的
      // if (self.custom_acceptEventInterval <= 0) {
      //     // 如果没有自定义时间间隔,则默认为2秒
      //    self.custom_acceptEventInterval = 2;
      // }
                    
    // 是否小于设定的时间间隔
     BOOL needSendAction = (NSDate.date.timeIntervalSince1970 - self.custom_acceptEventTime >= self.custom_acceptEventInterval);
                    
       // 更新上一次点击时间戳
          if (self.custom_acceptEventInterval > 0) {
              self.custom_acceptEventTime = NSDate.date.timeIntervalSince1970;
           }
                    
       // 两次点击的时间间隔小于设定的时间间隔时,才执行响应事件
           if (needSendAction) {
               [self custom_sendAction:action to:target forEvent:event];
           }

}

@end
</pre></code>

简书的一次试水,文章参考自简书ocarol

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 好奇触摸事件是如何从屏幕转移到APP内的?困惑于Cell怎么突然不能点击了?纠结于如何实现这个奇葩响应需求?亦或是...
    Lotheve阅读 58,372评论 51 604
  • 在iOS开发中经常会涉及到触摸事件。本想自己总结一下,但是遇到了这篇文章,感觉总结的已经很到位,特此转载。作者:L...
    WQ_UESTC阅读 6,154评论 4 26
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,235评论 30 472
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,099评论 19 139
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,242评论 6 13