objectiveccoder001
objectiveccoder001

Reputation: 3031

Running multiple CADisplayLinks at a time ?

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;
    }

}

Upvotes: 0

Views: 1225

Answers (2)

bturner
bturner

Reputation: 514

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.

Upvotes: 0

Jack Freeman
Jack Freeman

Reputation: 1414

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?

Upvotes: 2

Related Questions