iOS应用程序秒表和计时器在后台停止计数

时间:2013-11-16 16:52:31

标签: ios objective-c

我有一个iPhone应用程序,基本上是一个时钟,秒表和计时器。一切正常,直到你离开应用程序,秒表和计时器停止计数。我认为解决这个问题的方法是在将应用程序放入后台时启动计数器,并在应用程序再次启动时从秒表/计时器中添加/减去该计数器。这是最好的事情,如果是这样,我该怎么做?

更新:计时器,秒表和时钟都在不同的视图控制器中。这是我在StopwatchViewController.m中的秒表的代码:

- (void)stopwatch // Start counting the stopwatch
{
    hourInt = [hourLabel.text intValue];     // Store integer values of time
    minuteInt = [minuteLabel.text intValue];
    secondInt = [secondLabel.text intValue];

    if (secondInt == 59) {                   // Add on one second
        secondInt = 0;
        if (minuteInt == 59) {
            minuteInt = 0;
            if (hourInt == 23) {
                hourInt = 0;
            } else {
                hourInt += 1;
            }
        } else {
            minuteInt += 1;
        }
    } else {
        secondInt += 1;
    }

    NSString *hourString = [NSString stringWithFormat:@"%02d", hourInt];     // Update text to show time
    NSString *minuteString = [NSString stringWithFormat:@"%02d", minuteInt];
    NSString *secondString = [NSString stringWithFormat:@"%02d", secondInt];

    hourLabel.text = hourString;
    minuteLabel.text = minuteString;
    secondLabel.text = secondString;

    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(stopwatch) userInfo:nil repeats:NO]; // Repeat every second
}

如何让它添加应用程序在后台运行的时间?

2 个答案:

答案 0 :(得分:1)

添加两个观察者,以便在应用程序在后台输入和为应用程序输入enterForeGround

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterInBackGround) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterInForeground) name:UIApplicationWillEnterForegroundNotification object:nil];


- (void)applicationWillEnterInBackGround{
      // stop your timer here
}

- (void)applicationWillEnterInForeground
{
     // start your timer here
}

示例------

in .h

NSTimer *timer;
float time;

in .m

- (void)viewDidLoad
{
    [super viewDidLoad];

    timer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(stopwatch) userInfo:nil repeats:YES];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterInBackGround) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterInForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void)applicationWillEnterInBackGround
{
    [timer invalidate];
}

- (void)applicationWillEnterInForeground
{
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(stopwatch) userInfo:nil repeats:YES];
}

- (void)stopwatch // Start counting the stopwatch
{
    hourInt = [hourLabel.text intValue];     // Store integer values of time
    minuteInt = [minuteLabel.text intValue];
    secondInt = [secondLabel.text intValue];

    if (secondInt == 59) {                   // Add on one second
        secondInt = 0;
        if (minuteInt == 59) {
            minuteInt = 0;
            if (hourInt == 23) {
                hourInt = 0;
            } else {
                hourInt += 1;
            }
        } else {
            minuteInt += 1;
        }
    } else {
        secondInt += 1;
    }

    NSString *hourString = [NSString stringWithFormat:@"%02d", hourInt];     // Update text to show time
    NSString *minuteString = [NSString stringWithFormat:@"%02d", minuteInt];
    NSString *secondString = [NSString stringWithFormat:@"%02d", secondInt];

    hourLabel.text = hourString;
    minuteLabel.text = minuteString;
    secondLabel.text = secondString;
}

答案 1 :(得分:0)

最简单的方法是在应用程序进入后台时继续并保存当前时间,然后在返回前台时计算时间间隔并将其添加到计数器中,例如:

.. going background ..

NSDate *backgroundTime = [[NSDate date] retain];

.. returning foreground ..

NSTimeInterval elapsed = [NSDate timeIntervalSinceDate:date];
[date release];
yourTimer -= elapsed;

要了解您何时应该挂钩这些事件,您应该看看here