ios 避免UIbutton快速點選造成多次響應的避免方法
阿新 • • 發佈:2019-02-11
1、可以使用:
- (void) timeEnough
{
UIButton *btn=(UIButton*)[self.view viewWithTag:33];
btn.selected=NO;
[timer invalidate];
timer=nil;
}
- (void) btnDone:(UIButton*)btn
{
if(btn.selected) return;
btn.selected=YES;
[self performSelector:@selector(timeEnough) withObject:nil afterDelay:3.0];
//to do something.
}
2、個人覺得這種方法更為好用些。
- (void)todoSomething:(id)sender
{
//在這裡做按鈕的想做的事情。
}
- (void)starButtonClicked:(id)sender
{
//先將未到時間執行前的任務取消。
[[selfclass] cancelPreviousPerformRequestsWithTarget:selfselector:@selector(todoSomething:) object:sender];
[self performSelector
}