0

I am designing a timer that I want to be able to pause and continue from the same place. I tried doing it but the code wouldn't work for me. Here is my code:

.m file:

-(IBAction)buttonPause:(id)sender {
    NSString *dateString = timer.text;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.SS"];
    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:dateString];
    dateFromString = startDate;
}

- (IBAction)buttonStart:(id)sender {
    startDate = [NSDate date];
    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/100.0
                                                          target:self
                                                        selector:@selector(updateTimer)
                                                        userInfo:nil
                                                         repeats:YES];
}

- (void)updateTimer{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.SS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString = [dateFormatter stringFromDate:timerDate];
    timer.text = timeString;
}

My problem appears to lie in the buttonPause method. What am I doing wrong or am not doing? Thank you in advanced.

cory ginsberg
  • 2,907
  • 6
  • 25
  • 37
  • 2
    In order to avoid duplicates: take a look at this post: http://stackoverflow.com/questions/8101231/stopwatch-using-nstimer-incorrectly-includes-paused-time-in-display – tilo May 06 '12 at 20:40
  • I implemented that code, but I get the error: _Invalid operands to binary expression: (NSTimeInterval (AKA 'double') and NSTimerInterval (aka 'double'))_ on the line: `timeInterval += secondsAlreadyRun;`. – cory ginsberg May 06 '12 at 23:41
  • Are both timeInterval and secondsAreadyRun of type `NSTimeInterval` (without a `*` !)? – tilo May 07 '12 at 07:43

1 Answers1

0

You can use NSTimeInterval instead of timer. I have a functional code to pause and stop the timer.

@interface PerformBenchmarksViewController () {

    int currMinute;
    int currSecond;
    int currHour;
    int mins;
    NSDate *startDate;
    NSTimeInterval secondsAlreadyRun;
}

@end

- (void)viewDidLoad
{
    [super viewDidLoad];

    running = false;
}

- (IBAction)StartTimer:(id)sender {

    if(running == false) {

        //start timer
        running = true;
        startDate = [[NSDate alloc] init];
        startTime = [NSDate timeIntervalSinceReferenceDate];
        [sender setTitle:@"Pause" forState:UIControlStateNormal];
        [self updateTime];
    }
    else {
        //pause timer
        secondsAlreadyRun += [[NSDate date] timeIntervalSinceDate:startDate];
        startDate = [[NSDate alloc] init];
        [sender setTitle:@"Start" forState:UIControlStateNormal];
        running = false;
    }
}

- (void)updateTime {

    if(running == false) return;

    //calculate elapsed time
    NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
    NSTimeInterval elapsed = secondsAlreadyRun + currentTime - startTime;

    // extract out the minutes, seconds, and hours of seconds from elapsed time:
    int hours = (int)(mins / 60.0);
    elapsed -= hours * 60;
    mins = (int)(elapsed / 60.0);
    elapsed -= mins * 60;
    int secs = (int) (elapsed);

    //update our lable using the format of 00:00:00
    timerLabel.text = [NSString stringWithFormat:@"%02u:%02u:%02u", hours, mins, secs];

    //call uptadeTime again after 1 second
    [self performSelector:@selector(updateTime) withObject:self afterDelay:1];
}

Hope this will help. Thanks

Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57