iOS 定时器Timer不走

timer-线程问题

问题原因:

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

解决方法:

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


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

2、把timer添加到支线程

[NSThread detachNewThreadSelector:@selector(bannerStart) toTarget:self withObject:nil];
- (void)bannerStart{
         _shufflingFigureTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(shufflingFigureTimerAction:) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_shufflingFigureTimer forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容