IOS 计时器暂停和开始 防止重复点击

时间:2022-07-21 03:57:43
-(IBAction)btnClick{
[self starTimer];//开始计时
//[self stopTimer];
} -(NSTimer*)timer{
if (!_timer) {
_timer =[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(beginChange) userInfo:nil repeats:YES];
}
return _timer;
} //暂停定时器(只是暂停,并没有销毁timer)
-(void)pauseTimer{
[self.timer setFireDate:[NSDate distantFuture]];
}
//继续计时
-(void)continueTimer{
[self.timer setFireDate:[NSDate distantPast]];
} //开始计时
-(void)startimer{
[self.timer fire];
}
//暂停并销毁
-(void)stopTimer{
[self.timer invalidate];
self.timer = nil;
} //开启定时器
-(void)starTimer{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(timerStar) object:nil];
[self performSelector:@selector(timerStar) withObject:nil afterDelay:1];
}