iOS 防止UIButton按钮重复点击

- (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__);
}

你可能感兴趣的:(iOS 防止UIButton按钮重复点击)