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
{
//先将未到时间执行前的任务取消。
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(todoSomething:) object:sender];
[self performSelector:@selector(todoSomething:) withObject:sender afterDelay:0.2f];
}
时间: 2024-10-18 00:23:17