简体   繁体   English

如何暂停和恢复我的NSTimer

[英]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. 我有一个已经可以工作的NSTimer,但是当您单击按钮时想暂停它。 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 我已经有了暂停按钮,并且打算继续计算在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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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