iOS 定时器Timer常见问题

最近有朋友问我使用NStimer遇见与ScrollView并存时存在主线程阻塞的问题,自己总结几种解决方法:


问题原因:

一般定时器timer都会被以默认模式default添加到主线程的runloop中,当scroll界面时,runloop接到信息处理事件,就会改变timer的运行模式到tracing,从而停止运行timer。


解决方法:

1、改变timer添加到runloop中的模式:

 [[NSRunLoop currentRunLoop] addTimer:_shufflingFigureTimer forMode:NSRunLoopCommonModes];

2、把timer添加到支线程

1 [NSThread detachNewThreadSelector:@selector(bannerStart) toTarget:self withObject:nil];
- (void)bannerStart{
_shufflingFigureTimer = [NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(shufflingFigureTimerAction:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_shufflingFigureTimer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
}
上一篇:iOS定时器NSTimer的使用方法


下一篇:iOS定时器、延迟执行