0

Please excuse my english, I'm not very fluent.

I have an application with 10 workouts, and i would like to give to each workout an amount of time, for ex : 1st workout : 30 seconds 2nd workout : 40 seconds

But for now, i only managed to give each workout the same amount of seconds for all workout..

This is what i used to work this :

#define EXERCISELENGTH 30
#define EXERCISELENGTH1 40
#define RESTLENGTH 10

- (void)tick
{
if (self.inCountdown && (countdown == 0)) {
    self.inCountdown = NO;
    self.inExercise = YES;
    countdown = EXERCISELENGTH;
    [self workoutUI];
    [self updateUI];
    workoutIndex++;
}

if (self.inExercise && (countdown == 0)) {
    self.inExercise = NO;
    self.inRest = YES;
    countdown = RESTLENGTH;
    [self restUI];
    [self updateUI];
}
if (self.inRest && (countdown == 0)) {
    self.inRest = NO;
    self.inExercise = YES;
    countdown = EXERCISELENGTH;
    [self workoutUI];
    [self updateUI];
    workoutIndex++;
    if (workoutIndex == 10) {
        [self doneUI];
        [self.timer invalidate];
        return;
    }
}
[self updateUI];
countdown--;
}

Any help would be appreciated.

Thank you

rmaddy
  • 314,917
  • 42
  • 532
  • 579
daviddev38
  • 33
  • 7

2 Answers2

0

you have many options:

If you want to call a workout method every 30 seconds, you use this:

  [NSTimer scheduledTimerWithTimeInterval:30.0
                                     target:self
                                   selector:@selector(workoutMethod)
                                   userInfo:nil
                                    repeats:YES];

If you want to call the workout method after 30 seconds, you use this:

 [self performSelector:@selector(workoutMethod) withObject:nil afterDelay:30.0];

If you want to start a countdown timer after you start the workout method, you use this example : CountDown Timer ios tutorial?

Community
  • 1
  • 1
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • I have already done that.. what i want to do is give each workout a specific amount of time. – daviddev38 Sep 17 '15 at 14:50
  • Well, Im confsed. What do you mean by giving each workout a specific amount of time? HOw is it different than running a count down timer after the workout starts ? – Teja Nandamuri Sep 17 '15 at 14:54
  • this is what happens with my current code : Workout 1 : 30 seconds Workout 2 : 30 seconds Workout 3 : 30 seconds But what i want to do is : Workout 1 : 20 seconds Workout 2 : 30 seconds Workout 3 : 40 seconds. – daviddev38 Sep 17 '15 at 14:57
0

If you want a countdown timer, you should set up a repeating NSTimer that triggers an action that updates the display every second or so. However, that action should calculate the time remaining using the system time. So, your code should:

  • get the current time and calculate the expected finish time according to the desired time period (20 sec, 30 sec, etc.)
  • start a repeating timer with a 1 sec interval

Your timer's action should:

  • get the current system time
  • compare to the expected finish time
  • update the display
  • invalidate itself if it has reached the finish time

What you don't want to do is to rely on the timer to trigger at exactly 1 second intervals. NSTimer's resolution is such that each 'tick' can be off by as much as 0.1 sec, which means that 30 ticks could be off by up to 3 seconds. Using the system time, e.g. CACurrentMediaTime(), will provide a much more accurate clock and avoid accumulation of error.

Caleb
  • 124,013
  • 19
  • 183
  • 272