PDA

View Full Version : How to Implement a Countup Timer




Stunner
Nov 11, 2009, 12:23 AM
Ey fellas, I am wondering how I would go about creating something in which the user taps a button, a timer starts counting(in seconds, if possible) and then the user taps the button again and it measures the amount of time that has passed. I have looked here: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Timers/Articles/usingTimers.html#//apple_ref/doc/uid/20000807-CJBJCBDE to no avail. I tried understanding what it said but it really just ended up convoluted things much more for me. If anyone could possibly provide an example of how this would be done, it would be greatly appreciated. Thanks in advance!



Troglodyte
Nov 11, 2009, 02:25 AM
When the button's pressed record the time using [NSDate date] and start an NSTimer that calls a method to update your counter, change the action method for the button to one for when the counter is to be stopped. In that method you use [NSDate date] again so you can work out the time elapsed and invaldiate the NSTimer.

Simples.

Stunner
Nov 11, 2009, 02:43 AM
When the button's pressed record the time using [NSDate date] and start an NSTimer that calls a method to update your counter, change the action method for the button to one for when the counter is to be stopped. In that method you use [NSDate date] again so you can work out the time elapsed and invaldiate the NSTimer.

Simples.

Ey, thanks for the response. Just a few questions though as I am relatively new to all of this. When I start NSTimer what happens exactly? A counter starts in the background or do I need to update the counter every run? And how would I go about changing the action method(as well as the button label) for the button? Is there any guide or tutorial you can link me to? Also, why would I call NSDate at all? Why not just use NSTimer alone? Thanks.

Troglodyte
Nov 11, 2009, 07:12 AM
You should actually read up on this rather than just asking here but anyway...


When I start NSTimer what happens exactly? A counter starts in the background or do I need to update the counter every run?

You set up an NSTimer to call a certain method at a certain freqeuncy and say if you want it to do it once or to repeat. It doesn't do anything to the counter.

And how would I go about changing the action method

You need these methods

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

Which UIButton inherits from UIControl
(as well as the button label) for the button?

Look at the UIButton documentation. It's pretty obvious.

Is there any guide or tutorial you can link me to?

No idea.
Also, why would I call NSDate at all? Why not just use NSTimer alone?

NSTimer doesn't store the time nor how long it's been in existence. You need to get the time the counter was started with [NSDate date] and keep it in a retained variable so you can work out the duration later.

Stunner
Nov 11, 2009, 11:47 PM
You should actually read up on this rather than just asking here but anyway...


You set up an NSTimer to call a certain method at a certain freqeuncy and say if you want it to do it once or to repeat. It doesn't do anything to the counter.

You need these methods

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

Which UIButton inherits from UIControl

Look at the UIButton documentation. It's pretty obvious.

No idea.

NSTimer doesn't store the time nor how long it's been in existence. You need to get the time the counter was started with [NSDate date] and keep it in a retained variable so you can work out the duration later.

Thanks a lot for the thorough response. I do look around on google as well as the documentation before posting up questions to this forum(believe it or not) but the documentation isn't always very clear and can be hard to sift through. All I need is a starting point so that I can drill down from there, which is exactly what you have provided me with, and thank you for that.

To get the differences between button taps I just ended up using:

[date2 timeIntervalSinceDate:date1]

Here is a link to the page I found it on(for those who are interested):
http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtDates.html

I think that is a simpler way than you had suggested, and it gives me what I want. I tried reading up on NSTimer methods but the lack of examples in the documentation is a real killer... Anyways thanks for all your help. :)