【问题标题】:quit the application and relaunch it again timer should resume time according to the allotted time退出应用程序并重新启动它计时器应根据分配的时间恢复时间
【发布时间】:2017-01-17 09:46:12
【问题描述】:

我有倒数计时器。如果我退出应用程序并重新启动它,计时器应根据分配的时间恢复时间。是否可以?

timer = [NSTimer scheduledTimerWithTimeInterval:1 
                 target:self 
                 selector:@selector(timerFired) 
                 userInfo:nil repeats:YES];

- (void)timerFired
{
    if((currMinute>0 || currSeconds>=0) && currMinute>=0)
    {
        if(currSeconds==0)
        {
            currMinute-=1;
            currSeconds=59;
        }
        else if(currSeconds>0)
        {
            currSeconds-=1;
        }
        if(currMinute>-1)


        timeRemain=[NSString stringWithFormat:@"%d%@%02d",currMinute,@":",currSeconds];

    }
    else
    {
       [timer invalidate];
    }
}

【问题讨论】:

  • 退出应用程序时将剩余时间保存到UserDefaults,然后在应用程序重新启动时读取该值。然后你可以继续你剩下的时间。如果您希望它继续计数,那么您可能希望将退出应用程序的时间保存到UserDefaults,然后根据当前时间进行一些计算。

标签: ios objective-c xcode nstimer countdowntimer


【解决方案1】:

试试这个。

-(void)timerFired
{
    NSInteger currMin = [[NSUserDefaults standardUserDefaults] integerForKey:@"currentMinute"];
    NSInteger currSec = [[NSUserDefaults standardUserDefaults] integerForKey:@"currentSeconds"];

    if((currMin>0 || currSec>=0) && currMin>=0)
    {
        if(currSec==0)
        {
            currMin-=1;
            currSec=59;
        }
        else if(currSec>0)
        {
            currSec-=1;
        }
        if(currMin>-1)


            timeRemain=[NSString stringWithFormat:@"%d%@%02d",currMin,@":",currSec];
            [[NSUserDefaults standardUserDefaults] setInteger:currMin forKey:@"currentMinute"];
            [[NSUserDefaults standardUserDefaults] setInteger:currSec forKey:@"currentSeconds"];


    }
    else
    {
        [timer invalidate];
    }
}

【讨论】:

  • 是的,没关系。但是重新启动它,计时器应该根据分配的时间恢复时间吗?如何查看重启时间?
  • 然后你必须在退出时保存日期和时间 (applicationWillTerminate) 并再次检查启动(didFinishLaunchingWithOptions) 然后你可以找到它之间的差异,这样你就可以计算你的恢复时间。
【解决方案2】:

根据您的要求,您需要存储未来日期,如下所述。

在您启动计时器时添加当前时间,并在您需要的时间添加您的分钟和秒。

例如:

currMinute = 2;
currSeconds = 30;

NSDate *dateFuture = [[NSDate date] dateByAddingTimeInterval:(currMinute*60)+currSeconds];

现在像这样将这个未来的日期存储到 NSUserDefaults 中

if (![[NSUserDefaults standardUserDefaults] objectForKey:@"EndDate"]) {
   [[NSUserDefaults standardUserDefaults] setObject:dateFuture forKey:@"EndDate"];
}

启动你的计时器:

timer = [NSTimer scheduledTimerWithTimeInterval:1
                                             target:self
                                           selector:@selector(timerFired)
                                           userInfo:nil repeats:YES];

注意:在开始计时器之前,请检查 UserDefaults 中是否存在结束日期 并且结束日期早于当前日期。

- (void)timerFired
{

    NSComparisonResult result = [[NSDate date] compare:[[NSUserDefaults standardUserDefaults] objectForKey:@"EndDate"]];
    if ( result == NSOrderedDescending) {
        [timer invalidate];
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"EndDate"];
    }else{
        NSTimeInterval secondsBetween = [[NSDate date] timeIntervalSinceDate:[[NSUserDefaults standardUserDefaults] objectForKey:@"EndDate"]];

        NSLog(@"There are %f secondsBetween in between the two dates.", secondsBetween);
    }
}

现在您可以在重新启动您的应用 N 次后获得您的剩余时间,您只会获得更新的时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 2021-01-04
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多