简体   繁体   中英

how I can pause and resume my NSTimer

I have a NSTimer that already works however want to pause it when you click a button. This button besides pause redirects me to my other view then when I get back to the home view he has to continue where you left off. I already have the pause button and I intend to continue counting calling a method on viewWillAppear

this is my code:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUp];
}

- (void) setUp {
    _startDate = [NSDate date];
    _timer = [NSTimer scheduledTimerWithTimeInterval: 1.0/100.0 target: self selector: @selector(timerUpdating) userInfo: nil repeats: true];
}

-(void)timerUpdating {

    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:_startDate];
    NSDate *timerDate  = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
    [dateFormater setDateFormat:@"mm:ss"];
    [dateFormater setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString = [dateFormater stringFromDate:timerDate];
    self.timerLabel.text = timeString;
    self.pauseTimeInterval = &(timeInterval);
}

- (IBAction)timerActionPause:(id)sender {
}
- (void)startTimer {
    if(_timer != nil) { // To make sure 
        [self stopTimer];
    }
    _timer = [NSTimer scheduledTimerWithTimeInterval: 1.0/100.0 target: self selector: @selector(timerUpdating) userInfo: nil repeats: true];
}

- (void)stopTimer {
    [_timer invalidate];
    _timer = nil;
}

- (void)resetTimer {
    [self stopTimer];
    [self startTimer];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM