利用 runtime,解决多次点击相同 button,导致重复跳转的问题-b
当app有点卡的时候,多次点击相同的button,经常出现,跳转了N次相同的界面(比如闲鱼)
解决办法
用运行时和分类,替换 UIControl 响应事件,根据响应的间隔时间来判断是否执行事件。
详细步骤
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的更多相关文章
- 多次快速点击相同button导致重复响应的问题
Button在开发中经常用到,但是如果在瞬间点击多次时会出现多次响应事件的问题,今天给大家分享一下解决方法. 方法一:在Button响应事件中禁止Button允许点击, -(void)buttonAc ...
- 利用RunTime解决由NSTimer导致的内存泄漏
NSTimer使用场景 用NSTimer来实现每隔一定时间执行制定的任务,例如最常见的广告轮播图,使用NSTimer实现这个功能很简单代码如下 NSTimer *_timer; _timer = [N ...
- Runtime应用防止按钮连续点击 (转)
好久之前就看到过使用Runtime解决按钮的连续点击的问题,一直觉得没啥好记录的.刚好今天旁边同时碰到这个问题,看他们好捉急而且好像很难处理,于是我先自己看看… 前面自己也学习了很多Runtime的东 ...
- 利用NSProxy解决NSTimer内存泄漏问题
之前写过一篇利用RunTime解决由NSTimer导致的内存泄漏的文章,最近和同事讨论觉得这样写有点复杂,然后发现有NSProxy这么好用的根类,根类,根类,没错NSProxy与NSObject一样是 ...
- 利用闭包解决for循环里onclick事件不能捕捉实时i值问题
问题描述 我们都知道,如果我们对于一组元素(相同的标签)同时进行onclick事件处理的时候(在需要获取到索引的时候),一般是写一个for循环,但是onclick是一个异步调用的,所以会带来一个问题, ...
- iOS - 利用runtime加深对基础知识的理解
利用runtime加深对基础知识的理解 如果对runtime需要学习,可以看这篇,以下仅作为学习笔记,相互交流. runtime的头文件: #import <objc/runtime.h> ...
- iOS中利用 runtime 一键改变字体
1.准备 我们新建一个项目名叫ChangeFont,然后我就随便找了个名叫loveway.ttf的字体库拖进去,里面的工程目录大概就是这样的 目录 现在我们就简单的直接在storyboard上拖了一个 ...
- iOS利用Runtime自定义控制器POP手势动画
前言 苹果在iOS 7以后给导航控制器增加了一个Pop的手势,只要手指在屏幕边缘滑动,当前的控制器的视图就会跟随你的手指移动,当用户松手后,系统会判断手指拖动出来的大小来决定是否要执行控制器的Pop操 ...
- 利用Readability解决网页正文提取问题
分享: 利用Readability解决网页正文提取问题 做数据抓取和分析的各位亲们, 有没有遇到下面的难题呢? - 如何从各式各样的网页中提取正文!? 虽然可以用SS为各种网站写脚本做解析, 但是 ...
随机推荐
- 使用webView制作浏览器
xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- Unix 网络编程(2)——TCP API
TCP C/S套接口函数一般调用过程及基本函数 如上图所示的TCP连接的基本过程.一般来说,服务器先于客户端运行,服务器程序运行的基本过程是: socket()函数创建服务器段socket. bind ...
- 关于JFace中的输入值(InputDialog)对话框类
格式: InputDialog(Shell parentShell, String dialogTitle, String dialogMessage, String initialValue, In ...
- android 按两次返回键退出应用
private long mExitTime; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCod ...
- npm更换淘宝镜像
镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): 1.通过config命令 npm config set registry https://regist ...
- Linux命令行程序和内建指令
摘录百度分类 文件系统 cat cd chmod chown chgrp cksum cmp cp du df fsck fuser ln ls lsattr lsof mkdir mount mv ...
- Android数据存储方式之SharedPreferences
Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数.使用SharedPreferences保存数据,其背后是用xml文件存放数 ...
- Asp.net基础知识
1.[项目结构] 1.1文件后缀: .cs 源文件(程序代码) .csproj 项目文件(管理文件项) .sln 解决方案文件(管理项目) .config ...
- Markdown 生成目录
<link rel="stylesheet" href="http://yandex.st/highlightjs/6.2/styles/googlecode.mi ...
- error RC1205: invalid code page
Get followings error and warnings when building project: error RC1205: invalid code pagewarning C400 ...