1. NSTimer 被UIScrollView暫停
當(dāng)界面同時(shí)存在有定時(shí)器和UIScrollView時(shí),比如:一個(gè)界面頂部是一個(gè)由定時(shí)器控制的無(wú)限輪播圖,下面是一個(gè)UIScrollView,這時(shí)你如果拖動(dòng)下面UIScrollView,定時(shí)器就會(huì)暫停,等UIScrollView的拖動(dòng)事件結(jié)束后定時(shí)器才會(huì)接著執(zhí)行。
當(dāng)你創(chuàng)建輸入源(Timer)的時(shí)候,需要將其分配給 runloop 中的一個(gè)或多個(gè)模式。改Timer只會(huì)在改特定模式下被觸發(fā)。
****下面的方法 timer 被添加到 NSDefaultRunLoopMode****
timer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(getNotificationNews)
userInfo:nil
repeats:YES];
****下面的方法 timer 被添加到 NSRunLoopCommonModes****
timer = [NSTimer timerWithTimeInterval:10.0
target:self
selector:@selector(getNotificationNews)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
-
NSDefaultRunLoopMode:將NSTimer添加到主線程N(yùn)SRunLoop的默認(rèn)模式下,只有主線程是默認(rèn)模式下才能執(zhí)行NSTimer(滾動(dòng)scrollView,RunLoop默認(rèn)進(jìn)入Tracking模式,所以NSTimer不會(huì)有效果)。 -
UITrackingRunLoopMode:將NSTimer添加到主線程N(yùn)SRunLoop的追蹤模式下,只有主線程是追蹤模式下才能執(zhí)行NSTimer。(例如滾動(dòng)scrollView的時(shí)候就會(huì)監(jiān)聽到計(jì)時(shí)器) -
NSRunLoopCommonModes:Common是一個(gè)表示,它是將NSDefaultRunLoopMode 和 UITrackingRunLoopMode標(biāo)記為了Common
所以,只要將 timer 添加到 Common 占位模式下,timer就可以在Default和UITrackingRunLoopMode模式下都能運(yùn)行