我使用 CADisplayLink 作为频闪计时器。
我有 2 个 CADisplayLinks:
主要的(在整个过程中运行):
SMPTELink = [CADisplayLink displayLinkWithTarget:self selector:@selector(onTick)];
SMPTELink.frameInterval = 1;
[SMPTELink addToRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
频闪灯(仅在频闪发生时运行):
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.
一次运行两个 CADisplayLink 是不是很糟糕?有时我的闪光灯看起来并不像我认为的那样平滑。这是我的 toggleStrobe 方法:
-(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;
}
}