UIScrollView pauses NSTimer until scrolling finish

2019-01-03 02:02发布

While a UIScrollView (or a derived class thereof) is scrolling, it seems like all the NSTimers that are running get paused until the scroll is finished.

Is there a way to get around this? Threads? A priority setting? Anything?

5条回答
聊天终结者
2楼-- · 2019-01-03 02:49

An easy & simple to implement solution is to do:

NSTimer *timer = [NSTimer timerWithTimeInterval:... 
                                         target:...
                                       selector:....
                                       userInfo:...
                                        repeats:...];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-03 02:53

This is the swift version.

timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
            NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
查看更多
我只想做你的唯一
4楼-- · 2019-01-03 02:57

You have to run another thread and another run loop if you want timers to fire while scrolling; since timers are processed as part of the event loop, if you're busy processing scrolling your view, you never get around to the timers. Though the perf/battery penalty of running timers on other threads might not be worth handling this case.

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-03 02:58

Yes, Paul is right, this is a run loop issue. Specifically, you need to make use of the NSRunLoop method:

- (void)addTimer:(NSTimer *)aTimer forMode:(NSString *)mode
查看更多
走好不送
6楼-- · 2019-01-03 03:02

For anyone using Swift 3

timer = Timer.scheduledTimer(timeInterval: 0.1,
                            target: self,
                            selector: aSelector,
                            userInfo: nil,
                            repeats: true)


RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)
查看更多
登录 后发表回答