iOS 定時器Timer不走

timer-線程問題

問題原因:

一般定時器timer都會被以默認(rèn)模式default添加到主線程的runloop中,當(dāng)滑動界面時,runloop接到信息處理事件,就會改變timer的運(yùn)行模式到tracing,從而停止運(yùn)行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];
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容