Uiscrollview Pauses Nstimer While Scrolling

UIScrollView pauses NSTimer until scrolling finishes

An easy & simple to implement solution is to do:

NSTimer *timer = [NSTimer timerWithTimeInterval:... 
target:...
selector:....
userInfo:...
repeats:...];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

UIScrollView pauses NSTimer while scrolling

An easy way to fix this is adding your NSTimer to the mainRunLoop.

[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

To remove a timer from all run loop modes on which it is installed, send an invalidate message to the timer.

(Swift) NSTimer Stop when Scrolling

I created a simple project with a scrollView and a label that is updated with an NSTimer. When creating the timer with scheduledTimerWithInterval, the timer does not run when scrolling.

The solution is to create the timer with NSTimer:timeInterval:target:selector:userInfo:repeats and then call addTimer on NSRunLoop.mainRunLoop() with mode NSRunLoopCommonModes. This allows the timer to update while scrolling.

Here it is running:

Demo .gif

Here is my demo code:

class ViewController: UIViewController {

@IBOutlet weak var timerLabel: UILabel!
var count = 0

override func viewDidLoad() {
super.viewDidLoad()

timerLabel.text = "0"

// This doesn't work when scrolling
// let timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true)

// Do these two lines instead:
let timer = NSTimer(timeInterval: 1, target: self, selector: "update", userInfo: nil, repeats: true)

NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
}

func update() {
count += 1
timerLabel.text = "\(count)"
}
}

Swift 3:

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

Swift 4, 5:

let timer = Timer(timeInterval: 1, target: self, selector: #selector(update), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: RunLoop.Mode.common)

UISlider Pauses when UIScrollView is Scrolled

Problem is that scrolling of UIScrollView causes suspension of NSTimer. So, it's not related with your UISlider.

To fix this, after initialiazation your NSTimer add timer to RunLoop.main for mode RunLoopMode.commonModes

RunLoop.main.add(yourTimer, forMode: RunLoopMode.commonModes)

NSTimer not fired when uiscrollview event occurs

iOS Applications run on an NSRunLoop. Each NSRunLoop has different modes of execution for different tasks. For example, the default nstimer is scheduled to run under the NSDefaultRunMode on the NSRunLoop. What this means however is that certain UIEvents, scrollviewing being one, will interrupt the timer, and place it on a queue to be run as soon as the event stops updating. In your case, in order to get the timer to not be interrupted, you need to schedule it for a different mode, namely NSRunLoopCommonModes, like so:

  self.myTimer =  [NSTimer scheduledTimerWithTimeInterval:280
target:self
selector:@selector(doStuff)
userInfo:nil
repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:self.myTimer forMode:NSRunLoopCommonModes];

This mode will allow your timer to not be interrupted by scrolling.
You can find more about this info here: https://developer.apple.com/documentation/foundation/nsrunloop
At the bottom you will see the definitions of the modes you can choose from. Also, legend has it, you can write your own custom modes, but few have ever lived to tell the tale im afraid.

Animation stops when scrolling in UIScrollView

You should add timer to another loop mode:

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:0.005 target:self selector:@selector(increaseDarknessHeight) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

Updated for Swift:

let timer = Timer.scheduledTimer(timeInterval: 0.005, target: self, selector: #selector(increaseDarknessHeight), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: .common)

Why timer stops when scrolling in UIWebView iPhone?

You have to add the timer to another RunLoopMode. Per default a timer is added to the NSDefaultRunLoopMode. That means that the timer is only executing when the app’s run loop is in NSDefaultRunLoopMode.

Now, when a user touches the screen (e.g. to scroll a UIScrollView) the run loop’s mode will be switched to NSEventTrackingRunLoopMode. And now, that the run loop is not in NSDefaultRunMode anymore, the timer will not execute. The ugly effect of that is, that timer is blocked whenever the user touches the screen. And that can be a looong time when the user is scrolling, because the timer is blocked until the scrolling completely stops. And when the user continues to scroll, the timer is blocked again.

Fortunately the solution to this problem is quite simple: You can add your timer to another NSRunLoopMode. When add the timer to NSRunLoopCommonModes it will execute in all run loop modes (that have been declared as a member of the set of “common” modes, to be precise). That means that the timer is not only working in NSDefaultRunLoopMode but also in NSEventTrackingRunLoopMode (when the user touches the screen).

So after you initialize your timer, add it to NSRunLoopCommonModes:

[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

UILabel updating stops during scrolling UIScrollView

I had recently the same trouble and found the solution here: My custom UI elements....

In short: while your UIScrollView is scrolling, the NSTimer is not updated because the run loops run in a different mode (NSRunLoopCommonModes, mode used for tracking events).

The solution is adding your timer to the NSRunLoopModes just after creation:

NSTimer *elapsedTimeTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 
target:self
selector:@selector(updateElapsedTimeLabel)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:elapsedTimeTimer
forMode:NSRunLoopCommonModes];

(The code comes from the post linked above).



Related Topics



Leave a reply



Submit