iOS 定时器Timer常见问题

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


问题原因:

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


解决方法:

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

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

2、把timer添加到支线程

1 [NSThread detachNewThreadSelector:@selector(bannerStart) toTarget:self withObject:nil];
2 - (void)bannerStart{
3          _shufflingFigureTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(shufflingFigureTimerAction:) userInfo:nil repeats:YES];
4         [[NSRunLoop currentRunLoop] addTimer:_shufflingFigureTimer forMode:NSDefaultRunLoopMode];
5         [[NSRunLoop currentRunLoop] run];
6 }
原文地址:https://www.cnblogs.com/tig666666/p/5760516.html