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.
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.
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];
Multiple uiscrollviews, issues with scrolling
Got it to work.. thanks to this post.. UIScrollView pauses NSTimer until scrolling finishes
NSTimer *timer = [NSTimer timerWithTimeInterval:...
target:...
selector:....
userInfo:...
repeats:...];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
http://bynomial.com/blog/?p=67
Related Topics
When to Use Dequeuereusablecellwithidentifier VS Dequeuereusablecellwithidentifier: Forindexpath
Dismiss Keyboard by Touching Background of Uitableview
Move a View Up Only When the Keyboard Covers an Input Field
What Are the Dangers of Method Swizzling in Objective-C
iPhone Sdk: Differencebetween Loadview and Viewdidload
Xcode iOS 8 Keyboard Types Not Supported
Passing Data Between View Controllers Using Segue
Ios 6: How to Restrict Some Views to Portrait and Allow Others to Rotate
How to Play a Local Video With Swift
How to Detect If App Is Being Built For Device or Simulator in Swift
How to Perform Wireless Debugging in Xcode 9 With iOS 11, Apple Tv 4K, etc
iOS Detect If User Is on an iPad
Swiftui Navigationlink Loads Destination View Immediately, Without Clicking