向 iOS 应用程序添加一个正在运行的计数显示计时器,例如时钟秒表? [英] Add a running countup display timer to an iOS app, like the Clock stopwatch?

查看:19
本文介绍了向 iOS 应用程序添加一个正在运行的计数显示计时器,例如时钟秒表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个应用程序来处理设备运动事件并以 5 秒为增量更新界面.我想向应用程序添加一个指示器,以显示应用程序运行的总时间.类似秒表的计数器,如原生 iOS 时钟应用程序,似乎是计算应用程序运行时间并将其显示给用户的合理方式.

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.

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

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

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

  • 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.

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

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 *)]

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

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

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

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.

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

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?

推荐答案

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

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

1) 安排一个计时器

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

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

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.

- (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];
}

这篇关于向 iOS 应用程序添加一个正在运行的计数显示计时器,例如时钟秒表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆