0

我有以下代码:

[NSTimer scheduleTimerWithTimeInterval:0.5 target:self 选择器:@selector(timerCount:) userInfo:nil repeats:YES];

-(void)timerCount:(NSTimer *)timer
{
    NSTimeInterval dt = [定时器时间间隔];
    // 做一点事
}

我得到的 NSTimeInterval 将是 0.5,这是我在 scheduleTimerWithInterval 上设置的时间间隔,这意味着每 0.5 秒将调用一次 timerCount。

但是我现在有一些东西作为时间戳,我想知道 NSTimer 是否会在每次精确 0.5 秒内调用 timerCount 方法。

4

2 回答 2

0
 aTimer = [NSTimer timerWithTimeInterval:(1.0) target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];

        NSRunLoop *runner = [NSRunLoop currentRunLoop];
        [runner addTimer:aTimer forMode: NSDefaultRunLoopMode];


- (void)timerFired:(NSTimer*)theTimer
 {

    if(condition)
    { 
        //timer terminated
        [theTimer isinValid];
}

}

于 2012-08-31T12:17:11.687 回答
0

您不会在主线程上使用 NSTimer 来实现这一点,因为计时器仅由事件循环调用。

如果您需要最大精度,只需创建一个带有自己的消息循环的线程并在那里安排计时器。

于 2011-02-04T09:21:20.993 回答