且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在iPhone App中创建倒数计时器

更新时间:2023-11-17 15:56:10

将时间戳记转换为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)