Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-30-2010, 08:42 AM   #1 (permalink)
Registered Member
 
Mjsdabeast's Avatar
 
Join Date: Oct 2009
Location: Illinois
Age: 16
Posts: 94
Mjsdabeast is on a distinguished road
Question can a timer do this?

Hi,

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.
Mjsdabeast is offline   Reply With Quote
Old 05-30-2010, 10:07 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Mjsdabeast View Post
Hi,

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:

Code:
NSTimeInterval start_time;
BOOL timer_running;
Then, write an action method

Code:
- (IBAction) click_button;
{
   NSTimerInterval elapsed_time;
   NSTimerInterval time_remaining;
   if (!timer_running)
   {
      start_time = [NSDate timeIntervalSinceReferenceDate];
      timer_running = TRUE;
   }
   else
   {
      timer_running = FALSE;
      elapsed_time = [NSDate timeIntervalSinceReferenceDate] - start_time;
      time_remaining = 60 - elapsed_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.
Duncan C is offline   Reply With Quote
Old 05-30-2010, 01:22 PM   #3 (permalink)
Registered Member
 
Mjsdabeast's Avatar
 
Join Date: Oct 2009
Location: Illinois
Age: 16
Posts: 94
Mjsdabeast is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
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:

Code:
NSTimeInterval start_time;
BOOL timer_running;
Then, write an action method

Code:
- (IBAction) click_button;
{
   NSTimerInterval elapsed_time;
   NSTimerInterval time_remaining;
   if (!timer_running)
   {
      start_time = [NSDate timeIntervalSinceReferenceDate];
      timer_running = TRUE;
   }
   else
   {
      timer_running = FALSE;
      elapsed_time = [NSDate timeIntervalSinceReferenceDate] - start_time;
      time_remaining = 60 - elapsed_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.
Mjsdabeast is offline   Reply With Quote
Old 06-01-2010, 04:04 PM   #4 (permalink)
Registered Member
 
Mjsdabeast's Avatar
 
Join Date: Oct 2009
Location: Illinois
Age: 16
Posts: 94
Mjsdabeast is on a distinguished road
Question

Quote:
Originally Posted by Mjsdabeast View Post
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.
Mjsdabeast is offline   Reply With Quote
Old 06-01-2010, 04:32 PM   #5 (permalink)
A Single-Serving Friend
 
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
Robert Paulson is on a distinguished road
Default

Quote:
... 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.

Consider saying thanks by buying my app. :]
Robert Paulson is offline   Reply With Quote
Old 06-01-2010, 05:18 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Robert Paulson View Post
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.
Then your button pressed method:

Code:
- (IBAction) click_button;
{
   NSTimerInterval elapsed_time;
   NSTimerInterval time_remaining;
   if (!timer_running)
   {
      start_time = [NSDate timeIntervalSinceReferenceDate];
      end_time = start_time + 60;
      timer_running = TRUE;
      display_timer = [NSTimer timerWithTimeInterval: 1 
         target: self 
         selector: @selector(display_time) 
         userInfo:nil 
         repeats: YES];
   }
   else
   {
      timer_running = FALSE;
      [display_timer invalidate];
      display_timer = nil;
      [self display_time];
   }
}


-(void) display_time;
{
      time_remaining = end_time  - [NSDate timeIntervalSinceReferenceDate];
      if (time_remaining <= 0)
      {
         //stop the timer, we're done.
         time_remaining = 0;
         [display_timer invalidate];
         display_timer = nil;
      }
      display_field.text  = [NSString stringWithFormat: @"Time remaining %.1f seconds", 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.


Regards,

Duncan C
WareTo
Check out our apps in the Apple App store
Duncan C is offline   Reply With Quote
Old 06-01-2010, 06:17 PM   #7 (permalink)
A Single-Serving Friend
 
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
Robert Paulson is on a distinguished road
Default

I didn't know that timers are that inaccurate. Thanks for the information and presenting an alternative solution.

Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.

Consider saying thanks by buying my app. :]
Robert Paulson is offline   Reply With Quote
Old 06-01-2010, 10:02 PM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Robert Paulson View Post
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.
Duncan C is offline   Reply With Quote
Old 06-01-2010, 10:11 PM   #9 (permalink)
Registered Member
 
Mjsdabeast's Avatar
 
Join Date: Oct 2009
Location: Illinois
Age: 16
Posts: 94
Mjsdabeast is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
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.
Then your button pressed method:

Code:
- (IBAction) click_button;
{
   NSTimerInterval elapsed_time;
   NSTimerInterval time_remaining;
   if (!timer_running)
   {
      start_time = [NSDate timeIntervalSinceReferenceDate];
      end_time = start_time + 60;
      timer_running = TRUE;
      display_timer = [NSTimer timerWithTimeInterval: 1 
         target: self 
         selector: @selector(display_time) 
         userInfo:nil 
         repeats: YES];
   }
   else
   {
      timer_running = FALSE;
      [display_timer invalidate];
      display_timer = nil;
      [self display_time];
   }
}


-(void) display_time;
{
      time_remaining = end_time  - [NSDate timeIntervalSinceReferenceDate];
      if (time_remaining <= 0)
      {
         //stop the timer, we're done.
         time_remaining = 0;
         [display_timer invalidate];
         display_timer = nil;
      }
      display_field.text  = [NSString stringWithFormat: @"Time remaining %.1f seconds", 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.


Regards,

Duncan C
WareTo
Check out our apps in the Apple App store
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.
Mjsdabeast is offline   Reply With Quote
Old 06-02-2010, 03:36 PM   #10 (permalink)
Registered Member
 
Mjsdabeast's Avatar
 
Join Date: Oct 2009
Location: Illinois
Age: 16
Posts: 94
Mjsdabeast is on a distinguished road
Question

Quote:
Originally Posted by Mjsdabeast View Post
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.
Mjsdabeast is offline   Reply With Quote
Reply

Bookmarks

Tags
nstimer, setting, timer

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 326
9 members and 317 guests
ajay123123, ashaman64, baja_yu, ChrisYates, HemiMG, newDev, Objective Zero, pkIDSF, Steven.C
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,113
Posts: 402,878
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:55 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0