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