Start a new timer with remaining time interval - ios

I have a Timer that fires every 1.0 seconds. Every time it fires, a TimeInterval variable gets set to Date.timeSinceReferenceDate, and when I pause the timer, another TimeInterval variable also gets set to Date.timeSinceReferenceDate and the timer is invalidated.
When I press the start button again, I want it to resume from where it left off (i.e. if you paused 0.5 seconds before it fired again, it should take 0.5 seconds for it to fire when you resume, not the 1.0 it currently does).
Here's how my code looks:
if (elapsedTime - beginTime) < 1.0 {
leftoverTime = elapsedTime - beginTime
timer = Timer.scheduledTimer(timeInterval: leftoverTime, target: self, selector: #selector(decreaseTime), userInfo: nil, repeats: false)
let fireDate = Date().addingTimeInterval(leftoverTime)
timer = Timer.init(fireAt: fireDate, interval: 1.0, target: self, selector: #selector(decreaseTime), userInfo: nil, repeats: true)
}
I imagine I need to do something with a Run Loop, but isn't there a way to just do a scheduled timer with a fire date? If I do the fire() method, it naturally only runs once and doesn't loop.
Hope someone can shine some light on this.
Thank you.
Edit
I have a UIViewPropertyAnimator
var animator: UIViewPropertyAnimator? = nil
When I start the timer, I first check if it's nil
animator == nil {
animator = UIViewPropertyAnimator(duration: TimeInterval(timeSec), curve: .linear) {
self.view.layoutIfNeeded()
}
}
And then the timer starts initially
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(decreaseTime), userInfo: nil, repeats: true)
In the method for decreasing time, I change the constraints and start the animation
self.timeTrailing.constant = screenSize / 2
self.timeLeading.constant = screenSize / 2
animator?.startAnimation()
At this point it's all good, but when I then hit the pause button and pause the animation (and invalidating the timer), resuming them means they're no longer in sync. I imagine it's because you won't hit an interval of exactly 0.1 when you press it, but rather 0.03, 0.07 etc.
animator?.pauseAnimation()
timer?.invalidate()
How do I ensure that they're always synced up, no matter when and how many times you pause it before it's finished?

Related

Better alternative to timer based animation for custom activity indicator?

I have a class that displays a custom indeterminate progress indicator. Each timer update it simply increments the rotation of a UIImageView using CGAffineTransformRotate.
This all works, however, I noticed that when it is running, the background process that it is waiting for runs 50% slower - which is a huge penalty. For instance; instead of taking say 20 seconds to complete the processing it takes 30 seconds. Can someone recommend a solution with less performance penalty?
func show() {
timer?.invalidate()
timer = NSTimer.scheduledTimerWithTimeInterval(0.03, target: self, selector: #selector(self.updateTimer(_:)), userInfo: nil, repeats: true)
}
func updateTimer(sender: NSTimer) {
iconView.transform = CGAffineTransformRotate(iconView.transform, 0.15)
}
Use Core Animation to animate the rotation. The window server will do all the work outside of your app's process.
let animation = CABasicAnimation(keyPath: "transform.rotation")
animation.fromValue = 0
animation.toValue = 2 * M_PI
animation.repeatCount = .infinity
animation.duration = 1.25
iconView.layer.addAnimation(animation, forKey: animation.keyPath)
I think you can use dispatch_source from #Rob's answer:
Do something every x minutes in Swift
Here is the code:
var timer: dispatch_source_t!
func startTimer() {
let queue = dispatch_queue_create("com.domain.app.timer", nil)
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue)
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 60 * NSEC_PER_SEC, 1 * NSEC_PER_SEC) // every 60 seconds, with leeway of 1 second
dispatch_source_set_event_handler(timer) {
// do whatever you want here
}
dispatch_resume(timer)
}
func stopTimer() {
dispatch_source_cancel(timer)
timer = nil
}

auto scroll using animated UIScrollView.setContentOffset with NSTimer

I'm writing a music player using Swift, but when I'm tried to auto scroll the lyric, I found something weird.
I use a NSTimer to trigger the scroll:
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "updateTime", userInfo: nil, repeats: true)
func upadate is:
let offset = lyricScrollView.contentOffset
lyricScrollView.setContentOffset(CGPoint(x: offset.x, y: offset.y + 25), animated: true)
but when I change the time interval to 0.1 like this:
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "updateTime", userInfo: nil, repeats: true)
I found that my scrollview doesn't scroll 10x faster than before, instead, it scrolls a little every 0.1 seconds, not 25 points every 0.1 seconds. If I set animated to false, the problem is gone.
Could someone tell me why this happens?

Delay timer after first fire

I have a timer in my application that triggers a certain event:
myTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self,
selector: "searchForDrivers:", userInfo:nil, repeats: true)
The first time the timer is triggered, it has a delay of 10 ms. I don’t want the delay on the first timer trigger, but I'd like the second trigger to be delayed by 10 ms. How can I achieve this?
First schedule the timer as you've done.
timer = NSTimer.scheduledTimerWithTimeInterval(
10.0, target: self,
selector: "searchForDrivers:",
userInfo: nil,
repeats: true
)
Then, immediately afterwards, fire timer.
timer.fire()
According to the documentation,
You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.
See the NSTimer Class Reference for more information.
One more way to do that is you can use two timers for that like:
var myTimer = NSTimer()
var secondTimer = NSTimer()
After that you can set two timers this way:
myTimer = NSTimer.scheduledTimerWithTimeInterval(0, target: self,
selector: "searchForTRuckDrivers", userInfo:nil, repeats: false)
secondTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self,
selector: "searchForTRuckDrivers", userInfo:nil, repeats: true)
You can set first timer with delay 0 which will not repeat again while set another timer with delay 10 and it will repeat again and again.
After that in method you can invalidate first timer this way:
func searchForTRuckDrivers() {
if myTimer.valid {
myTimer.invalidate()
}
}
This will remove first timer but second timer will call this method with delay.
Hope it will help.

Swift - UIProgressView is not smooth with NSTimer

So I am using an NSTimer to let the user know the app is working. The progress bar is set up to last 3 seconds, but when running, it displays in a 'ticking' motion and it is not smooth like it should be. Is there anyway I can make it more smooth - I'm sure just a calculation error on my part....
If anyone could take a look that would be great. Here is the code:
import UIKit
class LoadingScreen: UIViewController {
var time : Float = 0.0
var timer: NSTimer?
#IBOutlet weak var progressView: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()
// Do stuff
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector:Selector("setProgress"), userInfo: nil, repeats: true)
}//close viewDidLoad
func setProgress() {
time += 0.1
progressView.progress = time / 3
if time >= 3 {
timer!.invalidate()
}
}
}
Edit: A simple 3 second UIView animation (Recommended)
If your bar is just moving smoothly to indicate activity, possibly consider using a UIActivityIndicatorView or a custom UIView animation:
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
UIView.animateWithDuration(3, animations: { () -> Void in
self.progressView.setProgress(1.0, animated: true)
})
}
Make sure your progressView's progress is set to zero to begin with. This will result in a smooth 3 second animation of the progress.
Simple animated progress (Works but still jumps a bit)
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIProgressView_Class/#//apple_ref/occ/instm/UIProgressView/setProgress:animated:
func setProgress() {
time += 0.1
progressView.setProgress(time / 3, animated: true)
if time >= 3 {
timer!.invalidate()
}
}
Option with smaller intervals. (Not recommended)
Set your timer to a smaller interval:
timer = NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector:Selector("setProgress"), userInfo: nil, repeats: true)
Then update your function
func setProgress() {
time += 0.001
progressView.setProgress(time / 3, animated: true)
if time >= 3 {
timer!.invalidate()
}
}
For continues loader
timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(setProgress), userInfo: nil, repeats: true)
and
func setProgress() {
time += 0.001
downloadProgressBar.setProgress(time / 3, animated: true)
if time >= 3 {
self.time = 0.001
downloadProgressBar.progress = 0
let color = self.downloadProgressBar.progressTintColor
self.downloadProgressBar.progressTintColor = self.downloadProgressBar.trackTintColor
self.downloadProgressBar.trackTintColor = color
}
It's hard to say exactly what the problem is. I would like to see the output if you put a print line in setProgress to print a timestamp. Is it actually firing every tenth of a second? My guess is that it is not.
Why not? Well, the timer schedules a run loop task in the main thread to execute the code in setProgress. This task cannot run until tasks in front of it in the queue do. So if there are long running tasks happening in your main thread, your timer will fire very imprecisely. My first suggestion is that this is perhaps what is happening.
Here is an example:
You start a timer to do something every second.
Immediately after, you start a long running main thread task (for example, you try to write a ton of data to a file). This task will take five seconds to complete.
Your timer wants to fire after one second, but your file-writing is
hogging the main thread for the next four seconds, so the timer can't fire
for another four seconds.
If this is the case, then to solve the problem you would either need to move that main thread work to a background thread, or else figure out a way to do it while returning to the run loop periodically. For example, during your long running main thread operation, you can periodically call runUntilDate on your run loop to let other run loop tasks execute.
Note that you couldn't just increment the progress bar fill periodically during the long running main thread task, because the progress bar will not actually animate its fill until you return to the run loop.
What about proper way for animating changes: animateWithDuration:animations: or CABasicAnimation. You can use this for creating smooth animations

swift - how use FIRE() in NSTimer

I need a cycle of 5 times of 2.5 seconds in a single cycle, however, that the pressure of a button bait from single cycle without interrupting the scheduling.
I use method fire (with pressed button) for stoping a repeating timer without interrupting its regular firing schedule, but I don't know, subsequent cycles are shorter than the defined 2.5 seconds:
.......
#IBAction func buttonZeroPressed(sender: AnyObject) {
timer.fire()
++addWin
}
......
timer = NSTimer.scheduledTimerWithTimeInterval(2.5, target: self,
selector: Selector("levelOne"), userInfo:nil,repeats: true)
......
func levelOne() {
level = 1
count++
changePhoto()
if (count >= 5) {
timer.invalidate()
}
}
After the 3 pressed button, the timer seems to be 2.5 seconds, and I do not understand why!
The fire() method is how you start a timer that's been created but not scheduled. You want to use invalidate() to stop the timer.
#IBAction func buttonZeroPressed(sender: AnyObject) {
timer.invalidate()
++addWin
}
Your timer will start immediately since you call scheduledTimerWith.... If you wanted to create a timer without starting, you would use:
timer = NSTimer(timeInterval: 2.5, target: self, selector: Selector("levelOne"), userInfo:nil,repeats: true)

Resources