I've been using NSTimer to fade-in/fade-out some images in my app based on user-set frequency. For example, if user set frequency to 5 seconds, then every 5 seconds the following code will execute:
[UIView animateWithDuration:someInterval
delay:0
options:UIViewAnimationCurveEaseInOut
animations:
^{
// UI alpha = ... code here
}
// off...
completion:^(BOOL finished){
[UIView animateWithDuration:someOtherInterval
delay:yetAnotherValue
options:UIViewAnimationCurveEaseInOut
animations:
^{
// UI alpha = ... code here
}
completion:nil
];
}
];
(The exact code isn't important, just the overall idea of fade-in/fade-out.) However, as many on StackOverflow and various websites point out, using NSTimer results in stuttering animations because it isn't precisely tied to the frame rate. So I tried to use CADisplayLink instead:
// in viewDidAppear:
timer_count = 0;
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(checkTimer)];
displayLink.frameInterval = 1;
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
-(void)checkTimer
{
timer_count++;
if(timer_count >= 1500)
{
[self doFadeInOutAnimation];
timer_count = 0;
}
}
This isn't having the desired effect though; the images are just displayed in very rapid succession instead of waiting to fade-in/out every 5 seconds.
Any idea what's the proper way to do it?