Search code examples
iphonecocoacore-animationnstimer

What is the optimal value to the NSTimer object?


I'm writing some app. I have there some class with custom view. In the view there is timer that is invoking "setNeedsDisplayInRect" method. I need that timer to be very offensive for purposes of the animation (there is no very complex animation). I have set the value for the timer: 0.0001. Is that value not too big? The app is working correctly with it.


Solution

  • Don't use NSTimer for this. Use CADisplayLink. It will run at the optimal rate for the device.

    Set it up like this:

    self.displayLink = [self.view.window.screen displayLinkWithTarget:self selector:@selector(setNeedsDisplayForMyView)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    

    and define a method setNeedsDisplayForMyView:

    - (void)setNeedsDisplayForMyView
    {
        [self.view setNeedsDisplayInRect:self.dirtyRect];
    }