简体   繁体   English

倒数计时器不会在0处停止

[英]Countdown timer doesn't stop at 0

I have a timer which works perfectly if the timer is in the foreground. 我有一个计时器,如果计时器在前台,则可以很好地工作。 It decrements perfectly and stops at 0. However, when I tap the homebutton to go to the main screen, and then wait for the local notification to pop up, and then I tap the notification, the time interval turns into 4.2 billion (the limit for unsigned long int). 它完美地递减并在0处停止。但是,当我点击homebutton进入主屏幕,然后等待本地通知弹出,然后我点击通知时,时间间隔变为42亿(上限用于无符号的long int)。 Basically, it doesn't stop at 0. I'm not sure how to fix this. 基本上,它不会在0处停止。我不确定如何解决此问题。 I tried making it a regular NSInteger and checking if the interval went below 0 but I got the same results. 我尝试将其设置为常规NSInteger,并检查间隔是否低于0,但得到的结果相同。

-(IBAction)startTimer:(id)sender{
    if (!timer) {
        [startButton setTitle:@"Start" forState:UIControlStateNormal];
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
       date = [NSDate date];
    } else {
        [startButton setTitle:@"Stop" forState:UIControlStateNormal];
        anotherTimeInterval = testTask.timeInterval;
        [timer invalidate];
        timer = nil;
    }

}

-(void)timerAction:(NSTimer *)t
{
    NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:date];
    if (testTask.timeInterval > 0){
        NSError *error;
        if (![self.context save:&error]) {
            NSLog(@"couldn't save: %@", [error localizedDescription]);
        }
        NSUInteger seconds = (NSUInteger)round(anotherTimeInterval-interval);
        NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
                            seconds / 3600, (seconds / 60) % 60, seconds % 60];
        testTask.timeInterval = seconds;
        timerLabel.text = string;
        NSLog(@"%@", string);
    } else {
        NSLog(@"timer ended");
        [self.timer invalidate];
        self.timer = nil;
        [self timerExpired];
    }
}

-(void)applicationWillResignActive:(UIApplication *)application{
    if (timer){
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:testTask.timeInterval];
        localNotification.alertBody = @"Time is up";
        localNotification.alertAction = @"Ok";
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }
}

The %02u you're using in the stringWithFormat: parses the value as an unsigned int. 您在stringWithFormat:使用的%02u将值解析为一个无符号的int。

Also, and I think this is your problem, you're checking testTask.timeInterval in your if statement, but getting the number of seconds from the interval variable you get at the start of the method. 另外,我认为这是您的问题,您正在if语句中检查testTask.timeInterval,但是从方法开始时获得的interval变量中获取秒数。 So you're always checking the previous value, meaning you're always one second behind. 因此,您始终在检查先前的值,这意味着始终落后一秒。

Edit: You could do somehting like this: 编辑:您可以这样做:

NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:date];
if (anotherTimeInterval-interval > 0){
    ...
}

That way, you check the new value instead of the old one. 这样,您将检查值而不是旧值。

Hope this helps 希望这可以帮助

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

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