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

解决办法

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

详细步骤

  1. UIControl

    创建一个 UIControl 的分类

    Snip20160816_3.png

Snip20160816_4.png

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

//  UIControl+Custom.h
//  Created by ocarol on 16/8/16.
//  Copyright © 2016年 ocarol. All rights reserved.
//  
#import <UIKit/UIKit.h>  
@interface UIControl (Custom)
@property (nonatomic, assign) NSTimeInterval custom_acceptEventInterval;// 可以用这个给重复点击加间隔
@end

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

//  UIControl+Custom.m
//  Created by ocarol on 16/8/16.
//  Copyright © 2016年 ocarol. All rights reserved.
//
#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, IMP 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

利用 runtime,解决多次点击相同 button,导致重复跳转的问题-b的更多相关文章

  1. 多次快速点击相同button导致重复响应的问题

    Button在开发中经常用到,但是如果在瞬间点击多次时会出现多次响应事件的问题,今天给大家分享一下解决方法. 方法一:在Button响应事件中禁止Button允许点击, -(void)buttonAc ...

  2. 利用RunTime解决由NSTimer导致的内存泄漏

    NSTimer使用场景 用NSTimer来实现每隔一定时间执行制定的任务,例如最常见的广告轮播图,使用NSTimer实现这个功能很简单代码如下 NSTimer *_timer; _timer = [N ...

  3. Runtime应用防止按钮连续点击 (转)

    好久之前就看到过使用Runtime解决按钮的连续点击的问题,一直觉得没啥好记录的.刚好今天旁边同时碰到这个问题,看他们好捉急而且好像很难处理,于是我先自己看看… 前面自己也学习了很多Runtime的东 ...

  4. 利用NSProxy解决NSTimer内存泄漏问题

    之前写过一篇利用RunTime解决由NSTimer导致的内存泄漏的文章,最近和同事讨论觉得这样写有点复杂,然后发现有NSProxy这么好用的根类,根类,根类,没错NSProxy与NSObject一样是 ...

  5. 利用闭包解决for循环里onclick事件不能捕捉实时i值问题

    问题描述 我们都知道,如果我们对于一组元素(相同的标签)同时进行onclick事件处理的时候(在需要获取到索引的时候),一般是写一个for循环,但是onclick是一个异步调用的,所以会带来一个问题, ...

  6. iOS - 利用runtime加深对基础知识的理解

    利用runtime加深对基础知识的理解 如果对runtime需要学习,可以看这篇,以下仅作为学习笔记,相互交流. runtime的头文件: #import <objc/runtime.h> ...

  7. iOS中利用 runtime 一键改变字体

    1.准备 我们新建一个项目名叫ChangeFont,然后我就随便找了个名叫loveway.ttf的字体库拖进去,里面的工程目录大概就是这样的 目录 现在我们就简单的直接在storyboard上拖了一个 ...

  8. iOS利用Runtime自定义控制器POP手势动画

    前言 苹果在iOS 7以后给导航控制器增加了一个Pop的手势,只要手指在屏幕边缘滑动,当前的控制器的视图就会跟随你的手指移动,当用户松手后,系统会判断手指拖动出来的大小来决定是否要执行控制器的Pop操 ...

  9. 利用Readability解决网页正文提取问题

    分享: 利用Readability解决网页正文提取问题   做数据抓取和分析的各位亲们, 有没有遇到下面的难题呢? - 如何从各式各样的网页中提取正文!? 虽然可以用SS为各种网站写脚本做解析, 但是 ...

随机推荐

  1. MySQL(8):数值类型详细分析

    1.日期和时间类型 2.varchar和char 固定长度 (char) 或可变长度 (varchar) 字符数据类型.  例如: a char(10)b varchar(10)都存入'abc'a要求 ...

  2. Android-Opencv开发(一)配置环境

    先去官网下载android-opencv http://opencv.org/.

  3. Android_listView_BaseAdapter_downLoadImg

    layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  4. UNIX V6内核源码剖析——进程

    进程的概念 1. 什么是进程 2. 进程的并行执行 3. 进程的运行状态 4. 用户模式和内核模式 cpu具有2种模式——内核模式和用户模式,通过PSW来切换. 切换时, 映射到虚拟地址的物理内存区域 ...

  5. uva - 133 The Dole Queue(成环状态下的循环走步方法)

    类型:循环走步 #include <iostream> #include <sstream> #include <cstdio> #include <cstr ...

  6. 实现百度地图导航Demo的语音播报功能

    上文中实现了在本地导入百度地图导航Demo,那么在此基础上如何实现导航的语音播报呢? 一.为该应用申请语音播报(也叫注册) http://developer.baidu.com/map/index.p ...

  7. HTTP层 —— 控制器

    1.简介 将所有的请求处理逻辑都放在单个 routes.php 中显然是不合理的,你也许还希望使用控制器类组织管理这些行为.控制器可以将相关的 HTTP 请求封装到一个类中进行处理.通常控制器存放在 ...

  8. FontAwesome 奥森图标的学习

    很早之前,就看到大家在使用代码做出很漂亮的图标,但是觉得需求不是很大,所以就没有看,但是技多不压身,这次有时间来学习下. FontAwesome官方网站 1,下载文件包 里面有两个文件夹,css 和 ...

  9. JAXB - XML Schema Types, Defining Types for XML Elements With Content

    Content: A Value The content of an XML element may be some value, or one or more subordinate element ...

  10. 支持IE,FireFox,Chrome三大主流浏览器,通过js+Flash方式将table导出Excel文件

    今天在做项目的时候,遇到了前端下载Excel的功能,结果原先的代码,如下: function generate_excel(tableid) {        var table = document ...