在iPhone App中创建倒数计时器 [英] Create a countdown timer in iPhone App

查看:69
本文介绍了在iPhone App中创建倒数计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用中使用倒数计时器. 我有这个日期

I want to use a countdown timer in my app. I have this date

03-09-2011T20:54:18Z

03-09-2011T20:54:18Z

现在,我想在此日期使用倒数计时器.您能否建议是否有与此相关的任何方法.如果我不清楚,请随时告诉我.预先感谢.

Now I want to use countdown timer for this date. Can you please suggest if there is any method regarding this. I f I am not clear at any point please let me know. Thanks in advance.

推荐答案

将时间戳记转换为NSDate *(请参见

Turn that timestamp into an NSDate* (see here)

然后

NSTimeInterval interval = [timestamp timeIntervalSinceNow];

将始终为您提供从现在到您关心的日期之间的秒数.然后,您可以执行以下操作:

will always give you the number of seconds between now and the date you care about. Then you can do something like:

#define SECONDS_IN_MINUTE 60
#define SECONDS_IN_HOUR (SECONDS_IN_MINUTE * 60)
#define SECONDS_IN_DAY (SECONDS_IN_HOUR * 24)

NSInteger days, hours, minutes, seconds;
days = interval / SECONDS_IN_DAY;
interval %= SECONDS_IN_DAY;
hours = interval / SECONDS_IN_HOUR;
interval %= SECONDS_IN_HOUR;
minutes = interval / SECONDS_IN_MINUTE;
seconds %= SECONDS_IN_MINUTE;

如果您希望它每秒更新一次实时",则使用performSelector:withObject:afterDelay:并延迟您想要更新显示的任何时间(或者每次运行循环仅需0.0来进行更新)

If you want it to update "live" every second then use performSelector:withObject:afterDelay: with a delay of whatever period you want to update the display (or just 0.0 to do it every run loop)

这篇关于在iPhone App中创建倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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