当前位置:网站首页>Solve the problem of repeated clicking of button uibutton
Solve the problem of repeated clicking of button uibutton
2022-07-24 13:52:00 【Muzi string music】
1、 Set whether you can click Properties NO Do not click YES You can click on the enabled / userInteractionEnabled
Initialize a UIButton
UIButton *btn = [[UIButton alloc]init];
[btn setTitle:@"Start" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
btn.frame = CGRectMake(150, 200, 100, 40);
btn.layer.cornerRadius = 8.f;
btn.backgroundColor = [UIColor groupTableViewBackgroundColor];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
Click on the way
- (void)btnClick:(UIButton *)btn{
btn.enabled = NO;
dispatch_time_t t = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC));
dispatch_after(t, dispatch_get_main_queue(), ^{
btn.enabled = YES;
});
}
2、 Use runtime Exchange methods to achieve 2s You cannot click repeatedly in
Class implementation exchange method code
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIControl (Extension)
@end
NS_ASSUME_NONNULL_END
#import "UIControl+Extension.h"
#import <objc/runtime.h>
staticchar *const ExchangeInvalid = "exchangeInvalid"; // Can I click
@interface UIControl()
@property (nonatomic,assign)BOOL exchangeInvalid; // Can I click
@end
@implementation UIControl (Extension)
#pragma mark Exchange method
+ (void)load {
Method clickMethod = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
Method exchange_clickMethod = class_getInstanceMethod(self, @selector(exchange_sendAction:to:forEvent:));
method_exchangeImplementations(clickMethod, exchange_clickMethod);
}
#pragma mark Method of realizing exchange
- (void)exchange_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
if (!self.exchangeInvalid) {
self.exchangeInvalid = YES;
[self exchange_sendAction:action to:target forEvent:event];
[self performSelector:@selector(setExchangeInvalid:) withObject:@(NO) afterDelay:2]; // Set up 2 Seconds before clicking
}
}
#pragma mark @property attribute set/get Method
- (BOOL)exchangeInvalid {
return [objc_getAssociatedObject(self, ExchangeInvalid) boolValue];
}
- (void)setExchangeInvalid:(BOOL)exchangeInvalid {
objc_setAssociatedObject(self, ExchangeInvalid, @(exchangeInvalid), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
viewDidLoad Implementation code
UIButton *btn = [[UIButton alloc]init];
[btn setTitle:@"Start" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
btn.frame = CGRectMake(150, 200, 100, 40);
btn.layer.cornerRadius = 8.f;
btn.backgroundColor = [UIColor groupTableViewBackgroundColor];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = 0;
[self.view addSubview:btn];
- (void)btnClick:(UIButton *)btn{
NSLog(@"%ld",btn.tag);
btn.tag ++;
}
The print result shows

边栏推荐
- R语言epiDisplay包的kap函数计算Kappa统计量的值(总一致性、期望一致性)、对多个评分对象的结果进行一致性分析、评分的类别为多个类别、如果评分中包含缺失值则标准误及其相关统计量则无法计算
- Data Lake series articles
- Editor formula
- 在EXCEL表格中如何进行快速换行
- Aggregation measurement of robot swarm intelligence based on group entropy
- [untitled]
- 数据湖系列文章
- Network security -- Service Vulnerability scanning and utilization
- XSS white list
- 网络安全——函数绕过注入
猜你喜欢

Why are there "two abstract methods" in the functional interface comparator?

网络安全——WAR后门部署

position: -webkit-sticky; /* for Safari */ position: sticky;

Network security - error injection

HCIP第十三天

Swarm intelligence collaborative obstacle avoidance method inspired by brain attention mechanism

Simple order management system small exercise

Unity pedestrians walk randomly without collision

rhcsa第六次笔记

为什么函数式接口 Comparator 中有 “两个抽象方法”?
随机推荐
Flinktable & SQL (VII)
Data formatting widget
Network security - file upload whitelist bypass
An error is reported when using activiti to create a database table,
rhcsa第六次笔记
The R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graphs, uses the by parameter to specify the groupi
Chapter VI bus
网络安全——文件上传竞争条件绕过
How to generate expected data? Emory University and others' latest "deep learning controllable data generation" review, 52 page PDF, covering 346 documents, comprehensively expounds the controllable g
Aike AI frontier promotion (7.24)
微信小程序 TODO案例
R语言检验样本比例:使用prop.test函数执行单样本比例检验计算总体中成功样本比例p值的置信区间
Exploration of sustainable learning ability to support the application of ecological evolution of Pengcheng series open source large models
Uni app background audio will not be played after the screen is turned off or returned to the desktop
交换机链路聚合详解【华为eNSP】
MPLS中的包交换和标签交换
position: -webkit-sticky; /* for Safari */ position: sticky;
在LNMP架构中搭建Zabbix监控服务
R language uses the sum function of epidisplay package to calculate the descriptive statistical summary information of the specified variables in dataframe under different grouping variables, visualiz
网络安全——Cookie注入