NSTimer--UITableviewCell
大部分時間都會使用scheduledTimerWithTimeInterval:target:selector:userInfo:repeats
來創建Timer,并且大部分時間都是能正常觸發的。
但是當Timer和scrollView碰到一起的時候就會出現不能觸發的情況,當把Timer的實現改為
_currentTimer = [NSTimer timerWithTimeInterval:1
target:self
selector:@selector(repeatSEL)
userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:_currentTimer forMode:NSRunLoopCommonModes];
Timer就能完美的運行。雖然兩次Timer都是在主線程上運行的,和scrollViewy依舊使用同一個Runloop但是因為Mode的不同造成了不同的結果。scheduledTimerWithTimeInterval觸發是默認的ModeNSDefaultRunLoopMode
。
圖示可以看出為什么Timer必須加入到Runloop中。因為Timer是也是一種資源,這種資源想起作用必須加入到runloop中,同理如果Runloop中不包含任何資源,運行該Runloop就會立即退出。
另外需要注意的一點就是同一線程的Runloop在運行的時候,任意時刻只能處于同一種mode。所以只能當程序處于這種mode的時候,timer才能得到觸發事件的機會。
關于不同的Mode參看下圖