NSTimer not working while dragging a UITableView
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
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)
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.