Hi there,
I am using the CADisplayLink timer to flash the screen of an iPhone at a frequency of 30 Hz. Since the refresh rate of the device is 60 Hz, I just need to change the background color in each frame in order to end up with a white frame, then a black frame, then a white frame, then a black frame, etc.
However, if I start the app and leave it running for a couple of minutes and then stop it and go back to the home screen, there is a faint flashing effect that is most visible near the borders of the screen. I haven't been able to find a solution to this, as the effect continues even after restarting the iPhone. What's more, the flashing effect fades away with time and eventually disappears.
I suspect this bug is beyond my code, but I would be grateful if you could prove me wrong.
Here is the buttonClicked function in my ViewController.m file (I'm using a Start/Stop button to start and stop flashing), where I initialise and invalidate the CADisplayLink:
- (IBAction)buttonClicked:(id)sender {
if ([self.startButton.currentTitle isEqual:@"START"]) {
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[sender setTitle:@"STOP" forState:UIControlStateNormal];
}
else if ([self.startButton.currentTitle isEqual:@"STOP"]) {
[self.displayLink invalidate];
self.displayLink = nil;
self.view.backgroundColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1];
[sender setTitle:@"START" forState:UIControlStateNormal];
}
}
And this is the tick function that gets called each frame:
- (void)tick:(CADisplayLink *) displayLink {
if ([self.view.backgroundColor isEqual:[UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1]]) {
self.view.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1];
}
else {
self.view.backgroundColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1];
}
}