This question already has answers here:
How to change the NSTimeInterval of an NSTimer after X seconds?
(2 answers)
Closed 7 years ago.
What I am trying to do is increase the speed of an NSTimer in swift to increase the speed of a function gradually without making a million different NSTimers. If there is a way to do that, how do you? And if this is not the correct way of doing that, what would the best way be? Thank you.
You can destroy the timer and re-create it with a different interval when you want to increase its speed.
You can also have a single timer that runs at the fastest possible interval, and then ignore the callback unless time % some_number == 0 then gradually make some_number smaller and smaller.
Related
This question already has answers here:
How do I find the time interval remaining from NSTimer
(2 answers)
Closed 6 years ago.
I am creating a very simple game where a I need to use the amount of time left before the timer fires in seconds so that I am able to convert it into a double. I have thought about using .fireDate but I don't think there is an easy way to convert it to a double. Is there a simpler way?
timer.fireDate.timeIntervalSinceNow should return the number of seconds remaining until the timer fires as an NSTimeInterval which is a Double
This question already has answers here:
Format realtime stopwatch timer to the hundredth using Swift
(2 answers)
Closed 7 years ago.
I've got a 9 figure number that needs to be incremented by 500 each second, but i decided to increment the number each milliseconds and update the label that displays the number. I'm using a NSTimer but as i've read everywhere around they're not accurate nor meant to be. I've tried using CFAbsoluteTimeGetCurrent but couldn't get it to work. Simply using NSTimer yields and inaccurate value. The incrementation doesn't stop each time the user opens the app it simply adds up the value and starts incrementing again.
Any Ideas?
Update:
Even though most answers point in the right direction, i solved my issue a little bit different. Thanks to all who answered and Martin.
I used a CADisplayLink instead of a NSTimer and got pretty accurate and constant results. Now notice i say pretty because the results are not totally accurate, but since in my case i'm incrementing a 9 figure number they're not noticeable, and my numbers are corrected as soon as the view appears again.
You can get Accuracy
timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:#selector(countup)userInfo:nil repeats:YES];
Use a timer only as a trigger to update the screen, do not rely on the exact time between each fire.
Keep an NSDate which represents the start time and use the current date when the timer fires to calculate the difference and update the label.
Consider using CADisplayLink to update your interface — it gives you very accurate numbers of the time passed since the previous frame was drawn, so you can always keep your UILabel up to date, regardless of how high or low your framerate is.
The Time won't be very accurate - but it doesn't need to be, if you get the accurate current time each time you go into the loop and add 500 x (whole number of seconds), you will get a display that increases by 500 each second (plus / minus 50-100 milliseconds)
The advantage of this approach is that you won't get an ever-increasing discrepancy in the timing, only ever 50-100 milliseconds.
If you want the timer to stop when the user switches out of the app, then you need to disable the timer when the app becomes inactive - have a look at this tutorial on the Ray Wenderlich site http://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial
I'm trying to implement a countdown feature for my program. It's a second-timer, so I use a NSTimer object with a time interval of 1.0 second to update the UI. But in order not to accumulate error (every 1.0-second interval will incur a little bit of lag), the program caculates the absolute difference between current time and beginning time for the remaining time displayed in the UI.
The problem is, after the NSTimer object runs for a significant time (say half an hour), it's no longer "synced" with the absolute time due to accumulated error: the UI update happens between two "absolute" seconds. For example, if the countdown starts at 00:00:00.000, at first UI updates at 00:00:01.000, 00:00:02.000 ... but after a while it becomes 00:30:03.567 or something like that.
Any idea how I can deal with this? Are there any other better ways to implement this? Thanks!
One high level idea is to detect when the timer is getting too far out of sync based on your absolute time calculation. When it gets past a specific threshold, say 0.01 seconds or whatever you desire, cancel the current timer and start a new one after an appropriate delay that gets it back "in sync".
This question already has an answer here:
Smooth damp or tween algorithm
(1 answer)
Closed 8 years ago.
I am trying to make an FPS but I need help on how to do smooth damp on my gun. It currently follows the mouse's position exactly but I want it to take a second to get there. Like a delay. I need this in Lua and can't use libraries.
Yes! Did some thinking and came up with an algorithm. Basically, I took mouse's current position and then made an if statement saying that if the position changes then it takes that number and divides it by 5
I have an app where I flash words at a constant speed. Say it's set to 60 times a minute. Each word then shows for 1 second each. It was pretty easy to accomplish with NSTimer.
However, I want to make it a little more intelligent now. Longer words show for slightly longer than shorter words. I've figured out the math on how to calculate this, but I'm not sure how in Objective-C to present a word for say, 0.15 seconds, then another word for 0.18 seconds, then a third word for 0.04 seconds, etc., depending on the length of the word.
Would just using a delay be the best way to do this?
You could use performSelector to delay, but it isn't necessarily very easy to manage.
You could use NSTimer, repeating, and set the fireDate for each new update required. This is relatively expensive but less so than repeatedly creating new timers.
You could use CADisplayLink with a combination of duration and frameInterval to get updates at multiples of the screen refresh rate. This should probably be the most performant and accurate.
But, overall, you shouldn't worry about performance until you have some evidence of a problem and / or have done some profiling. Think instead about what features you need and how easy they are to implement with each solution.