可能的原因是多次调用了scheduledTimerWithTimeInterval及[[NSRunLoopcurrentRunLoop]addTimer方法。
1.多次调用前提前销毁,保证主线程中只有一个NSTimer在运行即可。@property (nonatomic,assign)int time;
@property (nonatomic,assign)NSTimer *timer;
if ([_timer isValid]) {
[_timer invalidate];
}
_timer =nil;
self.time = (int)sender.value*60;
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scrollTimer:) userInfo:nil repeats:YES];
[[NSRunLoopcurrentRunLoop] addTimer:_timer forMode:UITrackingRunLoopMode];
-(void)scrollTimer:(NSTimer *)theTimer{
self.time =self.time-1;
int minutes =self.time /60;
int seconds =self.time - minutes *60;
if (self.time <0) {
_timerLabel.text = [NSStringstringWithFormat:@"定时:"];
[self changeSpeech:_stopBtn];
return;
}
_timerLabel.text = [NSStringstringWithFormat:@"%d:%d",minutes,seconds];
NSLog(@"%@",_timerLabel.text);
}
2.使用完毕后及时销毁
-(void)changeSpeech:(UIButton *)sender
{
if ([_timer isValid]) {
[_timer invalidate];
}
_timer =nil;
}