iOSInterviewQuestions icon indicating copy to clipboard operation
iOSInterviewQuestions copied to clipboard

30. 以+ scheduledTimerWithTimeInterval...的方式触发的timer,在滑动页面上的列表时,timer会暂定回调,为什么?如何解决?

Open CodingPub opened this issue 7 years ago • 0 comments

最终答案中只需要

//创建 Timer,然后添加到 NSRunLoopCommonModes
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0
     target:self
     selector:@selector(timerTick:)
     userInfo:nil
     repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

就能解决 Timer 计时会被 scrollView 的滑动影响。

增加下面的代码会导致无滑动事件时,计时器回调两次,因为 NSRunLoopCommonModes 包括了 UITrackingRunLoopMode 和 NSDefaultRunLoopMode

//将timer添加到NSDefaultRunLoopMode中
[NSTimer scheduledTimerWithTimeInterval:1.0
     target:self
     selector:@selector(timerTick:)
     userInfo:nil
     repeats:YES];

CodingPub avatar Sep 05 '18 02:09 CodingPub