0

I'm using CADisplayLink as a timer for strobing.

I have 2 CADisplayLinks:

The main one (this runs during the whole thing):

SMPTELink = [CADisplayLink displayLinkWithTarget:self selector:@selector(onTick)];
SMPTELink.frameInterval = 1;
[SMPTELink addToRunLoop:[NSRunLoop mainRunLoop]
                       forMode:NSDefaultRunLoopMode];

The strobe one (this only runs when strobing occurs):

strobeLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(toggleStrobe)];
strobeLink.frameInterval = 1;
[strobeLink addToRunLoop:[NSRunLoop mainRunLoop]
                     forMode:NSDefaultRunLoopMode];
[strobeLink setPaused:YES]; // I setPaused:NO when using the strobe.

Is it bad to run two CADisplayLinks at once? Sometimes my strobe doesn't look as smooth as I think it should be. Here is my toggleStrobe method:

-(void)toggleStrobe {

    if (!self.firstTimestampStrobe)
        self.firstTimestampStrobe = strobeLink.timestamp;

    NSTimeInterval elapsed = (strobeLink.timestamp - self.firstTimestampStrobe);

    NSInteger frameNumber = (NSInteger)(elapsed * ((strobeValue*15)/255)) % 2;

    if (frameNumber != self.lastFrameStrobe)
    {
        if (frameNumber == 1) {

            UIColor *color = [[UIColor alloc] initWithRed: 0 green: 0 blue: 0 alpha: 1.0];
            strobeBackground.backgroundColor = color;

        } else {

            UIColor *color = [[UIColor alloc] initWithRed: 0 green: 0 blue: 0 alpha: 0];
            strobeBackground.backgroundColor = color;

        }

        self.lastFrameStrobe = frameNumber;
    }

}
Omarj
  • 1,151
  • 2
  • 16
  • 43
objectiveccoder001
  • 2,981
  • 10
  • 48
  • 72

2 Answers2

2

The point of the display link is to be able to draw with each refresh of the screen. I don't see why you would need two(as it will still just get called when the display refreshes). Can't you just use the one and with each refresh determine what color to display?

Jack Freeman
  • 1,414
  • 11
  • 18
  • Did figure out a way to work it into one CADisplayLink - works great! – objectiveccoder001 Feb 22 '13 at 05:09
  • From a API-perspective it makes sense to not increase the complexity by checking if others already exists or not. – hfossli Dec 09 '13 at 19:15
  • 1
    It's the most obvious thing in the world that you would have two, three, or *dozens* of concepts which use frame loop. Saying "why use more than one CADisplaylnk callback" would be like saying "why have more than one function ini a class, just put it all in one." – Fattie Aug 21 '19 at 16:26
0

Its not bad.. its just pointless.

Why do you need two? The purpose of the CADisplayLink is to fire a method with every refresh of the display. If you want two different things to happen at two different rates then have the display link fire one method called refreshView: or something along those lines. Then in this method do your custom logic to determine if its time to toggle the strobe.

bturner
  • 514
  • 3
  • 9
  • From a API-perspective it makes sense to not increase the complexity by checking if others already exists or not. – hfossli Dec 09 '13 at 19:15