我不知道大家在使用NSTimer的时候有没有发现如果我们滑动ScrollView,NSTimer就会失效。我曾经遇到过这样的问题,但需求是即使滑动scrollView,NSTimer也不能失效,于是就好好地研究了下这个问题。最终发现是NSTimer选择的一个模式的问题。
代码:
[[NSRunLoopcurrentRunLoop] addTimer:self.timerforMode:NSDefaultRunLoopMode];
我们将timer添加到当前的主线程中,并且选择 NSDefaultRunLoopMode这个默认的模式。在选择这个默认的模式之后,如果我们不与UI进行交互那么NSTimer是有效的,如果我们与UI进行交互那么主线程runloop就会转到 UITrackingRunLoopMode模式下,不能处理定时器,从而定时器失效。让定时器不失效的方式有两种:1.改变runloop的模式(NSRunLoopCommonModes),无论用户是否与UI进行交互主线程的runloop都能处理定时器。2.开启一个新的线程,让定时器在新的线程中进行定义,这时定时器就会被子线程中的runloop处理。
代码:
改变runloop的模式:
self.timer = [NSTimertimerWithTimeInterval:1.0target:selfselector:@selector(updateTimer)userInfo:nilrepeats:YES];
// 添加到运行循环 NSRunLoopCommonModes:占位模式 主线程
[[NSRunLoopcurrentRunLoop] addTimer:self.timerforMode:NSRunLoopCommonModes]; // 如果不改变Mode模式在滑动屏幕的时候定时器就不起作用了
在子线程中定义定时器:
[NSThreaddetachNewThreadSelector:@selector(bannerStart)toTarget:selfwithObject:nil];
// 在子线程中定义定时器
- (void)bannerStart{
self.timer = [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(updateTimer)userInfo:nilrepeats:YES];
[[NSRunLoopcurrentRunLoop] addTimer:self.timerforMode:NSDefaultRunLoopMode];
[[NSRunLoopcurrentRunLoop] run];
}
- (void)updateTimer
{
NSLog(@"当前线程:%@",[NSThread currentThread]);
NSLog(@"启动RunLoop后--%@",[NSRunLoop currentRunLoop].currentMode);
NSLog(@"currentRunLoop:%@",[NSRunLoop currentRunLoop]);
dispatch_async(dispatch_get_main_queue(), ^{
self.count ++;
NSString *timerText = [NSString stringWithFormat:@"计时器:%ld",self.count];
self.timerLabel.text = timerText;
});
}