Okay, I have searched online and even looked in a couple of books for the answer because I can't understand the apple documentation for the NSTimer. I am trying to implement 2 timers on the same view that each have 3 buttons (START - STOP - RESET).
The first timer counts down from 2 minutes and then beeps.
The second timer counts up from 00:00 indefinitely.
I am assuming that all of the code will be written in the methods behind the 3 different buttons but I am completely lost trying to read the apple documentation. Any help would be greatly appreciated.
On your iPhone or iPad, swipe down from the top-right corner of the screen to open Control Center. If you're using an older iPhone, swipe up from the bottom of the screen instead. Now, press and hold the Timer button. (Simply tapping the Timer icon will open the Timer section in the Clock app.)
Basically what you want is an event that fires every 1 second, or possibly at 1/10th second intervals, and you'll update your UI when the timer ticks.
The following will create a timer, and add it to your run loop. Save the timer somewhere so you can kill it when needed.
- (NSTimer*)createTimer { // create timer on run loop return [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTicked:) userInfo:nil repeats:YES]; }
Now write a handler for the timer tick:
- (void)timerTicked:(NSTimer*)timer { // decrement timer 1 … this is your UI, tick down and redraw [myStopwatch tickDown]; [myStopwatch.view setNeedsDisplay]; // increment timer 2 … bump time and redraw in UI … }
If the user hits a button, you can reset the counts, or start or stop the ticking. To end a timer, send an invalidate message:
- (void)actionStop:(id)sender { // stop the timer [myTimer invalidate]; }
Hope this helps you out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With