向 iOS 应用程序添加一个正在运行的计数显示计时器,例如时钟秒表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12076409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:48:54  来源:igfitidea点击:

Add a running countup display timer to an iOS app, like the Clock stopwatch?

iosobjective-ccocoa-touchtimenstimer

提问by Alex Stone

I'm working with an app that processes device motion events and updates interface in 5 second increments. I would like to add an indicator to the app that would display the total time the app has been running. It seems that a stopwatch-like counter, like the native iOS Clock app is a reasonable way to count time that the app has been running and display it to the user.

我正在使用一个应用程序,它以 5 秒的增量处理设备运动事件和更新界面。我想向应用程序添加一个指示器,以显示应用程序运行的总时间。看起来像秒表一样的计数器,比如原生 iOS 时钟应用程序是计算应用程序运行时间并将其显示给用户的合理方法。

What I'm not sure of is the technical implementation of such a stopwatch. Here's what I'm thinking:

我不确定这种秒表的技术实现。这是我的想法:

  • if I know how long between interface updates, I can add up seconds between events and keep a count of seconds as a local variable. Alternatively, a 0.5 second interval scheduled timer can provide the count.

  • If I know the start date of the app, I can convert the local variable to date for each interface update using [[NSDate dateWithTimeInterval:(NSTimeInterval) sinceDate:(NSDate *)]

  • I can use a NSDateFormatter with a short time style to convert the updated date to a string using stringFromDatemethod

  • The resulting string can be assigned to a label in the interface.

  • The result is that the stopwatch is updated for each "tick" of the app.

  • 如果我知道界面更新之间的时间间隔,我可以将事件之间的秒数相加,并将秒数作为局部变量。或者,一个 0.5 秒间隔的预定计时器可以提供计数。

  • 如果我知道应用程序的开始日期,我可以使用 [[NSDate dateWithTimeInterval:(NSTimeInterval) sinceDate:(NSDate *)]

  • 我可以使用具有短时间样式的 NSDateFormatter 使用stringFromDate方法将更新的日期转换为字符串

  • 结果字符串可以分配给界面中的标签。

  • 结果是秒表针对应用程序的每个“滴答”进行更新。

It appears to me that this implementation is a bit too heavy and is not quite as fluid as the stopwatch app. Is there a better, more interactive way to count up time that the app has been running? Maybe there's something already provided by iOS for this purpose?

在我看来,这个实现有点太重了,不像秒表应用那么流畅。有没有更好、更具交互性的方式来计算应用程序运行的时间?也许 iOS 已经为此提供了一些东西?

回答by terry lewis

If you look in the iAd sample code from Applein the basic banner project they have a simple timer:

如果您在基本横幅项目中查看Apple 的 iAd 示例代码,他们有一个简单的计时器:

NSTimer *_timer; 
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];

and the the method they have

以及他们的方法

- (void)timerTick:(NSTimer *)timer
{
    // Timers are not guaranteed to tick at the nominal rate specified, so this isn't technically accurate.
    // However, this is just an example to demonstrate how to stop some ongoing activity, so we can live with that inaccuracy.
    _ticks += 0.1;
    double seconds = fmod(_ticks, 60.0);
    double minutes = fmod(trunc(_ticks / 60.0), 60.0);
    double hours = trunc(_ticks / 3600.0);
    self.timerLabel.text = [NSString stringWithFormat:@"%02.0f:%02.0f:%04.1f", hours, minutes, seconds];
}

It just runs from start up, pretty basic.

它从一开始就运行,非常基本。

回答by danh

Almost what @terry lewis suggested but with an algorithm tweak:

几乎是@terry lewis 建议的,但有一个算法调整:

1) schedule a timer

1)安排一个计时器

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

2) when the timer fires, get the current time (that's the tweak, don't count ticks because if there is wobble in the timer, tick counting will accumulate the error), then update the UI. Also, NSDateFormatter is a simpler and more versatile way to format time for display.

2) 当计时器触发时,获取当前时间(这就是调整,不要计算滴答,因为如果计时器中有摆动,滴答计数会累积错误),然后更新 UI。此外,NSDateFormatter 是一种更简单、更通用的方式来格式化时间以进行显示。

- (void)timerTick:(NSTimer *)timer {
    NSDate *now = [NSDate date];

    static NSDateFormatter *dateFormatter;
    if (!dateFormatter) {
        dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"h:mm:ss a";  // very simple format  "8:47:22 AM"
    }
    self.myTimerLabel.text = [dateFormatter stringFromDate:now];
}