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

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

问题描述

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



我不知道的是这样的秒表的技术实现。这是我的想法:




  • 如果我知道接口更新之间有多长时间,作为局部变量的秒计数。


  • 如果我知道应用程序的开始日期,我可以将本地变量转换为date每个接口更新使用 [[NSDate dateWithTimeInterval:(NSTimeInterval)sinceDate:(NSDate *)]


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


  • p>


  • 结果是,应用程序的每个tick都会更新秒表。




我觉得这个实现有点太重了,不像秒表应用那么流畅。有没有更好的,更互动的方式来计算应用程序已经运行的时间?也许iOS已经提供了一些东西用于这个目的?

解决方案

@terry lewis建议使用算法tweak:



1)计划计时器

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

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; //非常简单的格式8:47:22 AM
}
self.myTimerLabel.text = [dateFormatter stringFromDate:now];
}


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:

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

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?

解决方案

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

1) schedule a timer

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.

- (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天全站免登陆