Search code examples
iosnstimernsrunloop

how to get a accurate timer in ios


I am doing an app which needs a timer. I have tried NSTimer but NSTimer always loses or gets delayed. Is there a class in the iOS SDK which can log time accurately. An accuracy between 20ms and 40ms is okay. I'd like to be able to change an image in a fixed time interval.

NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval:.04f target:self    selector:@selector(timer1) userInfo:nil repeats:YES];
- (void)timer1
{
   NSLog(@"timer1 timer %@",[NSDate date]);
}

Solution

  • NSTimer is not a high-resolution timer. From the NSTimer class documentation:

    Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds.

    In addition to link suggested by @CJ Foley, you can check out dispatch_after function.