您的位置:首页 > 产品设计 > UI/UE

iOS 防止UIButton按钮重复点击

2017-08-10 10:06 615 查看
- (void)viewDidLoad{
[super viewDidLoad];

// 1. 创建 btn 并添加点击事件
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(100, 100, 100, 60)];
btn.backgroundColor = [UIColor greenColor];
[btn addTarget:self action:@selector(clickbtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}


// 2. 延时时间设置为 t (此处设置为1.0s) ,所以凡是前后两次点击时间间隔不超过1.0s都会被取消
- (void)clickbtn:(UIButton *)btn{
NSLog(@" %s ",__FUNCTION__);
[[self class] cancelPreviousPerformRequestsWithTarget:self
selector:@selector(handleEvent:)
object:btn];
[self performSelector:@selector(handleEvent:) withObject:btn afterDelay:1.0];
}


// 3. 点击事件的处理方法
- (void)handleEvent:(UIButton *)btn{

NSLog(@" %s ",__FUNCTION__);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: