暂停后恢复计时器?

时间:2014-05-06 08:54:17

标签: ios objective-c nstimer

我看过其他暂停/恢复计时器问题,但无法弄清楚如何解决我的问题。我只有一个简单的计时器标签和两个按钮,一个开始和一个停止按钮。如果您需要有关我的项目的更多信息以帮助回答我的问题,请告诉我们。谁能看到我出错的地方?

- (void)updateTimer
{
    // Create date from the elapsed time
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];

    // Create a date formatter
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];

    // Format the elapsed time and set it to the label
    NSString *timeString = [dateFormatter stringFromDate:timerDate];
    self.stopwatchLabel.text = timeString;
}

- (void)updateResumeTimer
{
    // Format the elapsed time and set it to the label
    NSString *timeString = [dateFormat stringFromDate:dateFor];
    self.stopwatchLabel.text = timeString;
}

- (IBAction)onStartPressed:(id)sender {

NSString *isItEqual = self.stopwatchLabel.text;

if ([isItEqual isEqualToString:@"00:00:00"]) {
    self.startDate = [NSDate date];

    // Create the stop watch timer that fires every 10 ms
    self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                           target:self
                                                         selector:@selector(updateTimer)
                                                         userInfo:nil
                                                          repeats:YES];
}
else {
    // Create the stop watch timer that fires every 10 ms
    self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                           target:self
                                                         selector:@selector(updateResumeTimer)
                                                         userInfo:nil
                                                          repeats:YES];

}
}


- (IBAction)onStopPressed:(id)sender {
resumeText = self.stopwatchLabel.text;
 NSLog(@"Timer is %@", resumeText);

dateFormat=[[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"HH:mm:ss"];
dateFor=[dateFormat dateFromString:resumeText];
[dateFormat setDateFormat:@"HH:mm:ss"];

[self.stopWatchTimer invalidate];
self.stopWatchTimer = nil;

[self updateTimer];
}

1 个答案:

答案 0 :(得分:1)

您需要添加属性

@property (assign, nonatomic) NSTimeInterval previousTimeInterval;

只需复制以下代码行即可。

- (void)updateTimer
{
    // Create date from the elapsed time
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.startDate];
    timeInterval += self.previousTimeInterval;
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    self.dateFor = timerDate;

    // Create a date formatter
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];

    // Format the elapsed time and set it to the label
    NSString *timeString = [dateFormatter stringFromDate:timerDate];

    self.stopwatchLabel.text = timeString;
}

- (IBAction)onStartPressed:(id)sender {

    self.startDate = [NSDate date];

    // Create the stop watch timer that fires every 10 ms
    self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                        target:self
                                                     selector:@selector(updateTimer)
                                                     userInfo:nil
                                                repeats:YES];

   //Disable start button and enable stop
    self.startButton.enabled = NO;
    self.stopButton.enabled = YES;
}


- (IBAction)onStopPressed:(id)sender
{
    [self.stopWatchTimer invalidate];
    self.stopWatchTimer = nil;

    self.previousTimeInterval = [self.dateFor timeIntervalSince1970];
    // enable start button and disable stop button
    self.startButton.enabled = YES;
    self.stopButton.enabled = NO;
}