lakshmen
lakshmen

Reputation: 29064

Start and Stop functionality of StopWatch is not coordinated iOS

I am creating a stopwatch and the start and stop functionality is not coordinated. When the timer stops, it doesn't start where it left of.

The code looks like this:

self.endDate = [NSDate dateWithTimeIntervalSinceNow:12.0f*60.0f];

-(void)startPressed:(id)sender{
    if(!_running){
        _running = TRUE;
        [sender setTitle:@"Stop" forState:UIControlStateNormal];
        if (_stopTimer == nil) {
            _stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                         target:self
                                                        selector:@selector(changeTimer:)
                                                       userInfo:nil
                                                        repeats:YES];
        }
    }else{
        _running = FALSE;
        [sender setTitle:@"Start" forState:UIControlStateNormal];
        [_stopTimer invalidate];
        _stopTimer = nil;
    }

}

- (void)changeTimer:(NSTimer*)timer {
    _timeInterval = [self.endDate timeIntervalSinceNow];
    self.timerControl1.minutesOrSeconds = ((NSInteger)_timeInterval)%60;
    self.timerControl2.minutesOrSeconds = (NSInteger)(_timeInterval/60.0f);
    self.timerControl3.minutesOrSeconds = ((NSInteger)_timeInterval)%60;
}

I know it is because of timeIntervalSinceNow. But, how to change this? Need some guidance on this.

The problem:

When you stop the stopwatch and start it again after 3s for example, it shld still start back from the same timing. but both my code, the start and stop timing are different, they are starting from a different timing. Need help to solve this.

Upvotes: 0

Views: 46

Answers (1)

Daij-Djan
Daij-Djan

Reputation: 50089

don't use the endDate in changeTimer but use the endDate only to init the timer

#import "RimsViewController.h"

@interface RimsViewController ()
@property(weak) IBOutlet UILabel *label;
@property(strong) NSDate *endDate;
@end

@implementation RimsViewController {
    BOOL _running;
    NSTimeInterval _timeInterval;
    NSTimer *_stopTimer;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.endDate = [NSDate dateWithTimeIntervalSinceNow:10*60];
    _timeInterval = [self.endDate timeIntervalSinceNow];
}

-(IBAction)startPressed:(id)sender{
    if(!_running){
        _running = TRUE;
        [sender setTitle:@"Stop" forState:UIControlStateNormal];
        if (_stopTimer == nil) {
            _stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                          target:self
                                                        selector:@selector(changeTimer:)
                                                        userInfo:nil
                                                         repeats:YES];
        }
    }else{
        _running = FALSE;
        [sender setTitle:@"Start" forState:UIControlStateNormal];
        [_stopTimer invalidate];
        _stopTimer = nil;
    }

}

- (void)changeTimer:(NSTimer*)timer {
    _timeInterval -= 1;// [self.endDate timeIntervalSinceNow];

    NSUInteger secs = _timeInterval;
    int h = (NSUInteger)secs/3600;
    secs-=h*3600;
    int m = (NSUInteger)secs/60;
    secs-=m*60;
    int s = secs;

    self.label.text = [NSString stringWithFormat:@"%d / %d / %d", h, m, s];
}

@end

Upvotes: 1

Related Questions