恢复功能在NSTime中无法正常工作

时间:2015-07-24 15:19:39

标签: ios objective-c

我有一个NSTimer,可以作为秒表使用。它使时间计数并具有正常工作的暂停功能,但该功能不能继续工作。计时器似乎计数甚至暂停,但没有更新标签,当我触摸按钮时,从你离开的地方继续,它继续计数,并没有停止。我该如何解决这个问题?

这是我的天文台代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUp];
    self.isPaused = NO;

}

- (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];
    if (self.isPaused == NO){
        _timerLabel.text = timeString;
    }
}


- (IBAction)pauseAction:(id)sender {
    [self pauseTimer:self.timer];
}

- (IBAction)resumeAction:(id)sender {
    [self resumeTimer:self.timer];
}

-(void) pauseTimer:(NSTimer *)timer {
    self.isPaused = YES;
    self.pauseStart = [NSDate date];
    self.previousFireDate = [timer fireDate];
    [timer setFireDate:[NSDate distantFuture]];
}

-(void) resumeTimer:(NSTimer *)timer {
    self.isPaused =  NO;
    NSDate *currentDate = [NSDate date];
    NSTimeInterval pauseTime = [currentDate timeIntervalSinceDate:pauseStart];
    NSDate *neededFireDate = [NSDate dateWithTimeInterval:pauseTime sinceDate:previousFireDate];
    [timer setFireDate:neededFireDate];
    NSLog(@"Fire Date: %@", timer.fireDate);
}

1 个答案:

答案 0 :(得分:1)

timerUpdating,您设置了NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];。如果用户暂停计时器,则不会考虑这一点。

要纠正此行为,您可以执行以下操作:

在.h(设置成员变量)中:

NSTimeInterval pausedTime;

在.m(编辑3行)

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUp];
    self.isPaused = NO;
    pausedTime = 0;    //edit
}

- (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] - pauseTime;//edit
    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];
    if (self.isPaused == NO){
        _timerLabel.text = timeString;
    }
}


- (IBAction)pauseAction:(id)sender {
    [self pauseTimer:self.timer];
}

- (IBAction)resumeAction:(id)sender {
    [self resumeTimer:self.timer];
}

-(void) pauseTimer:(NSTimer *)timer {
    self.isPaused = YES;
    self.pauseStart = [NSDate date];
    self.previousFireDate = [timer fireDate];
    [timer setFireDate:[NSDate distantFuture]];
}

-(void) resumeTimer:(NSTimer *)timer {        
    self.isPaused =  NO;
    NSDate *currentDate = [NSDate date];
    NSTimeInterval pauseTime = [currentDate timeIntervalSinceDate:pauseStart];
    pausedTime += pauseTime;//edit
    NSDate *neededFireDate = [NSDate dateWithTimeInterval:pauseTime sinceDate:previousFireDate];
    [timer setFireDate:neededFireDate];
    NSLog(@"Fire Date: %@", timer.fireDate);
}