如何防止用户滚动表格导致NSTimer延迟?
我找到了答案:
我有一个计时器,重复约8或9次,间隔为0.4到0.8秒.我不需要太多的精度,但如果用户滚动表,计时器将停止工作,直到表完成滚动(这可能是几秒钟等待!).我以为我需要后台线程,但是后台线程上的定时器实现起来有些复杂.
我的问题的答案非常简单容易.我只需要在调用计时器后添加一行:
//////////// start the timer
self.playingTimer = [NSTimer scheduledTimerWithTimeInterval:tempo target:self selector:@selector(playSoundFromArray:) userInfo:nil repeats:YES];
//////////// the magic line:
[[NSRunLoop currentRunLoop] addTimer:self.playingTimer forMode:UITrackingRunLoopMode];
Run Code Online (Sandbox Code Playgroud)
现在我可以根据需要滚动表格,我的计时器工作正常!
现在我需要再研究一下NSRunLoop ......