joe*_*ebo 1 nstimer nsrunloop ios
我正在做一个需要计时器的应用程序。我已经尝试过,NSTimer但NSTimer总是会丢失或延迟。iOS SDK中是否有一个可以准确记录时间的类。20ms到40ms之间的精度是可以的。我希望能够在固定的时间间隔内更改图像。
NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval:.04f target:self selector:@selector(timer1) userInfo:nil repeats:YES];
- (void)timer1
{
NSLog(@"timer1 timer %@",[NSDate date]);
}
Run Code Online (Sandbox Code Playgroud)
使用CADisplayLink此。它以系统帧速率触发:60 fps或17 ms。
如果需要更长的时间间隔,可以将其设置为以每秒帧数的倍数触发,例如,每第二或第三帧。
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(timer1)];
displayLink.frameInterval = 2;
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
Run Code Online (Sandbox Code Playgroud)
它应用于应该每n帧更新一次的UI 。
不要使用dispatch_after。顾名思义,唯一的保证是您的块将在特定时间后执行。如果任何东西阻塞了线程,则您的阻塞将不得不等待。
| 归档时间: |
|
| 查看次数: |
5482 次 |
| 最近记录: |