NSTimer not working while dragging a UITableView

帅比萌擦擦* 提交于 2019-11-29 01:27:57

By using scheduledTimerWithTimeInterval:, as j.tom.schroeder says, your timer is automatically schedule on the main run loop for the default modes. This will prevent your timer from firing when your run loop is in a non-default mode, e.g., when tapping or swiping.

The solution, though, is not using a thread, but scheduling your timer for all common modes:

NSTimer *timer = [NSTimer timerWithTimeInterval:1
                                         target:self
                                       selector:@selector(actualizarTiempo:)
                                       userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

Depending on the kind of events you would like to allow without them stopping your timers, you might also consider UITrackingRunLoopMode. For more info about run loop modes, see Apple Docs.

Esqarrouth

Heres the Swift version:

Swift 2

var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "removeFromSuperview", userInfo: nil, repeats: false)
NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

Swift 3, 4, 5

var timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(removeFromSuperview), userInfo: nil, repeats: false)
RunLoop.main.add(timer, forMode: RunLoop.Mode.common)

This is normal behavior. scheduledTimerWithTimeInterval: schedules on the current run loop, so any interaction with the UITableView will block the actualizarTiempo: method from being called. Use the NSThread methods detachNewThreadSelector:toTarget:withObject and sleepForTimeInterval to perform actualizarTiempo: on a separate thread.

Swift version 4+:

var timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.TimerController), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)

if you want to repeat for example as a counter then set repeat = true else = false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!