I am trying to figure out if you can get a timer to start when a button is pushed and then count down from 60 seconds and then the user pushes it again when they think the 60 seconds is up. Also, use the timer to tell them if they were close to pushing it at exactly 60 seconds, or even have it say you were 3 seconds early.
If you can understand what i'm trying to say any response would be helpful.
I am trying to figure out if you can get a timer to start when a button is pushed and then count down from 60 seconds and then the user pushes it again when they think the 60 seconds is up. Also, use the timer to tell them if they were close to pushing it at exactly 60 seconds, or even have it say you were 3 seconds early.
If you can understand what i'm trying to say any response would be helpful.
It doesn't sound like a timer is the best choice for this application. A timer counts down for a fixed amount of time, then calls a method that you specify when it "fires."
It doesn't sound like you want that. You want to note the time when the user clicks a button, and then start counting the amount of seconds from that time.
Something like this:
First, in your header, declare a variable start_time:
When the user clicks the button the second time, the click_button method would calculate the amount of time that passed since the first click in the local variable elapsed_time, and the number of seconds remaining in time_remaining.
You would need to covert that to a string and display it to a label or other UI element.
If you want to update a running elapsed time display, you could create a repeating timer that fired every 1/2 second or so, and updated your elapsed time string each time it fired.
It doesn't sound like a timer is the best choice for this application. A timer counts down for a fixed amount of time, then calls a method that you specify when it "fires."
It doesn't sound like you want that. You want to note the time when the user clicks a button, and then start counting the amount of seconds from that time.
Something like this:
First, in your header, declare a variable start_time:
When the user clicks the button the second time, the click_button method would calculate the amount of time that passed since the first click in the local variable elapsed_time, and the number of seconds remaining in time_remaining.
You would need to covert that to a string and display it to a label or other UI element.
If you want to update a running elapsed time display, you could create a repeating timer that fired every 1/2 second or so, and updated your elapsed time string each time it fired.
Ok, thank you i will try that and get back to you soon, I still am having some problems getting back into making apps, but i'll try to get it working soon.
Ok, thank you i will try that and get back to you soon, I still am having some problems getting back into making apps, but i'll try to get it working soon.
Ya, that actually didn't work. I need the timer to start counting down the first time a button is pushed, then stop the next time that same button is pushed. Then i could set up a number value for when the timer stopped, or how long the timer went on for. If you know how to do that it would be great, or maybe i set this up wrong and it does work.
... count down from 60 seconds and then the user pushes it again when they think the 60 seconds is up.
From your initial question it seems to me as if you don't need a count down. Simply do this: Create an instance variable to keep track of the time. When the button is pressed, start a timer with an interval of one second. Every time the timer elapsed, a method gets called. In that method, increase the instance variable accordingly. When the button is pressed again, stop the timer and subtract 60 seconds from your instance variable.
The value of your instance variable will then tell you how close the user was to 60 seconds. Does that make sense? Or: Would that be what you want?
Hope this helps.
Cheers,
Bob
__________________ We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.
From your initial question it seems to me as if you don't need a count down. Simply do this: Create an instance variable to keep track of the time. When the button is pressed, start a timer with an interval of one second. Every time the timer elapsed, a method gets called. In that method, increase the instance variable accordingly. When the button is pressed again, stop the timer and subtract 60 seconds from your instance variable.
The value of your instance variable will then tell you how close the user was to 60 seconds. Does that make sense? Or: Would that be what you want?
Hope this helps.
Cheers,
Bob
Timers are not very accurate. Trying to bump a counter once every second based on a timer will cause a result that will tend to drift from the true amount of elapsed time.
Using an instance variable to record the starting time and then starting a timer to trigger counting the amount of time remaining is the way to go.
Here's what I would do:
In the header:
Code:
NSTimeInterval start_time;
NSTimeInterval end_time;
NSTimeInterval time_remaining;
BOOL timer_running;
NSTimer* display_timer;
IBOutlet UILabel* display_field.text; //This is the field that displays the time remaining.
Disclaimer: This code has not been compiled, much less debugged. It no doubt contains syntax errors, and possibly bugs. It is only meant as a rough guide. You will need to adapt it to your needs.
I didn't know that timers are that inaccurate. Thanks for the information and presenting an alternative solution.
Cheers,
Bob
Here is an excerpt from the docs in XCode on timers:
Quote:
A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.
Actually, reading further, it sounds like I was wrong about the timer drifting. It sounds like a repeating timer's cycle interval is based on the start time, not the time of the last firing. The delay between firings will vary slightly, but it should "correct itself". However, if the run loop is busy, it can miss one or more scheduled firing times.
Again, from the XCode docs on NSTimer:
Quote:
A repeating timer always schedules itself based on the scheduled firing time, as opposed to the actual firing time. For example, if a timer is scheduled to fire at a particular time and every 5 seconds after that, the scheduled firing time will always fall on the original 5 second time intervals, even if the actual firing time gets delayed. If the firing time is delayed so far that it passes one or more of the scheduled firing times, the timer is fired only once for that time period; the timer is then rescheduled, after firing, for the next scheduled firing time in the future.
Timers are not very accurate. Trying to bump a counter once every second based on a timer will cause a result that will tend to drift from the true amount of elapsed time.
Using an instance variable to record the starting time and then starting a timer to trigger counting the amount of time remaining is the way to go.
Here's what I would do:
In the header:
Code:
NSTimeInterval start_time;
NSTimeInterval end_time;
NSTimeInterval time_remaining;
BOOL timer_running;
NSTimer* display_timer;
IBOutlet UILabel* display_field.text; //This is the field that displays the time remaining.
Disclaimer: This code has not been compiled, much less debugged. It no doubt contains syntax errors, and possibly bugs. It is only meant as a rough guide. You will need to adapt it to your needs.
that makes sense with all the stuff about timers, but this code that you wrote, i haven't tried it out yet but does the timer stop the second time you hit that button? I wasn't sure. thanks for all your help.
that makes sense with all the stuff about timers, but this code that you wrote, i haven't tried it out yet but does the timer stop the second time you hit that button? I wasn't sure. thanks for all your help.
O.k. I tried out the code and fixed the little mistakes and have it running with no errors or warnings but it fails every time it gets up to the else statement. The second time you hit the button, it doesn't understand the [self display_time]; or something like that and it crashes. It never gets to -(void)display_time. Also, i understand that the timer does stop now after you hit it the second time, i just didn't look at it that carefully.