如何防止iOS上的同一UIButton的多个事件?

我想要防止在同一个UIButton上连续多次点击。

我尝试enabledexclusiveTouch属性,但它没有奏效。 如:

 -(IBAction) buttonClick:(id)sender{ button.enabled = false; [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{ // code to execute } completion:^(BOOL finished){ // code to execute }]; button.enabled = true; } 

你正在做的是,只需在块外设置启用/禁用。 这是错误的,它的执行一旦这个方法将调用,因此它不会禁用该button,直到完成块将调用。 相反,一旦你的animation完成,你应该重新启用它。

 -(IBAction) buttonClick:(id)sender{ button.enabled = false; [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{ // code to execute } completion:^(BOOL finished){ // code to execute button.enabled = true; //This is correct. }]; //button.enabled = true; //This is wrong. } 

哦,是的,而不是truefalseYESNO看起来不错。 🙂

这是我的解决scheme:

NSInteger _currentClickNum; //保存标签button的当前值被点击

 //Button click event - (void)tabBt1nClicked:(UIButton *)sender { NSInteger index = sender.tag; if (index == _currentClickNum) { NSLog(@"Click on the selected current topic, not execution method, avoiding duplicate clicks"); }else { [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(tabBtnClicked:) object:sender]; sender.enabled = NO; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ sender.enabled = YES; }); _currentClickNum = index; NSLog(@"Column is the current click:%ld",_currentClickNum); } }