的的NSDate方法,因为一月timeIntervalSinceReferenceDate返回的秒数手动调度1,2001,格林威治标准时间上午12:00。它包括的第二
分数如果你调用,除以60,取最低值,然后乘以60,它应该给你目前的“圆分钟”的时间间隔。将60加到上面,你得到下一个“round minute”的时间间隔,给THAT加1,在下一个“round minute”之后你得到一秒的时间间隔
代码可能看起来像这样的:
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval nextMinute = floor(now/60)*60 + 61 //time interval for next minute, plus 1 second
NSTimeInterval delay = nextMinute - now;
//Delay now contains the number of seconds until the next "round minute", plus 1 second.
dispatch_after(
dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(delay * NSEC_PER_SEC)),
dispatch_get_main_queue(),^
{
//Replace the code below with whatever target/userInfo you need.
[NSTimer scheduledTimerWithTimeInterval: 60
target: self
userInfo: nil;
repeats: YES];
}
);
编辑:其实,一分钟的“第一第二”是第零秒,所以你应该改变+61在计算nextMinute
为“+60”上面的代码,而不是“+61”
你需要每分钟的第一秒,而不是仅仅一分钟一次有什么特别的原因吗?只是问,因为你正在运行的当前设备上的时间不太可能是完全正确的。 – Fogmeister 2014-12-01 15:59:11
我认为问题是,反正是有,我们可以在正是我们所需要基于设备的时间的时间保证的东西执行。也想亲自了解这一点。 – Unheilig 2014-12-01 16:00:54
@Fogmeister我想更新自定义标签的分钟数,但希望在+ 2秒内正确。但不想每秒都打电话给NSTimer。我知道这是疯狂的:-) – WebOrCode 2014-12-01 16:07:40