0

I realize that a CADisplayLink would be better suited for the nature of my current project, however, i can't quite figure out how to implement a CADisplayLink and replace my NSTimer.

below is the code for my NSTimer

Movement = [NSTimer scheduledTimerWithTimeInterval:0.002 target:self selector:@selector(BarMoving)       userInfo:nil repeats:YES];

how can I create a CADisplayLink that will perform the same function but more efficiently?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

0

Create the thing:

_displayLink = [CADisplayLink displayLinkWithTarget:self 
                   selector:@selector(BarMoving)];

Start it running:

[_displayLink addToRunLoop:[NSRunLoop mainRunLoop]
    forMode:NSDefaultRunLoopMode];

... that'll cause your display link to issue calls to BarMoving on the main run loop (which is the one associated with the main thread and therefore the main queue) whenever that run loop is in the default mode. So things like when the user has their finger down scrolling a scroll view will pause your timer. NSTimer has the same default behaviour.

Tommy
  • 99,986
  • 12
  • 185
  • 204