Is iOS there is CADisplayLink, in Mac OS X there is CVDisplayLink, but I can\'t find a way to use it, all the examples are related to OpenGL.
I created this custom U
You can configure a CVDisplayLink to work independently of OpenGL. The following is code that I've used to set up a CVDisplayLink to trigger regular capture and rendering from an industrial camera:
CGDirectDisplayID displayID = CGMainDisplayID();
CVReturn error = kCVReturnSuccess;
error = CVDisplayLinkCreateWithCGDisplay(displayID, &displayLink);
if (error)
{
NSLog(@"DisplayLink created with error:%d", error);
displayLink = NULL;
}
CVDisplayLinkSetOutputCallback(displayLink, renderCallback, (__bridge void *)self);
my renderCallback
function looks something like this:
static CVReturn renderCallback(CVDisplayLinkRef displayLink,
const CVTimeStamp *inNow,
const CVTimeStamp *inOutputTime,
CVOptionFlags flagsIn,
CVOptionFlags *flagsOut,
void *displayLinkContext)
{
return [(__bridge SPVideoView *)displayLinkContext renderTime:inOutputTime];
}
You'll have to replace the above with your own class and callback method, of course. The __bridge
in the code is there for ARC support, so you may not need that in a manually reference counted environment.
To start capturing events from the display link, you'd use
CVDisplayLinkStart(displayLink);
and to stop
CVDisplayLinkStop(displayLink);
When done, be sure to clean up after your created CVDisplayLink using CVDisplayLinkRelease()
.
If I may make one last comment, the reason why you see CVDisplayLink normally paired with OpenGL is that you usually don't want to be doing rapid refreshes on the screen using Core Graphics. If you need to be animating something at a 30-60 FPS rate, you're going to either want to draw directly using OpenGL or use Core Animation and let it handle the animation timing for you. Core Graphics drawing is not the way to fluidly animate things.
I would think that an NSTimer would be the way to go here. Your statement "it doesn't have the same accuracy", is interesting. Perhaps what you are mistaking for an accuracy problem is a run loop mode problem. If you are seeing the timer not fire when you are doing certain things such as opening menus or dragging, you may need to just change the run loop modes that the timer is running in.
Another option is to calculate your animation based on the actual time that it's rendered, not on an assumed difference since the last render pass. Doing the latter will visually exaggerate any delays in rendering that might be otherwise go unnoticed.
Aside from NSTimer, you can also do timers in GCD. Take a look at this example: http://www.fieryrobot.com/blog/2010/07/10/a-watchdog-timer-in-gcd/
We use CVDisplayLink for OpenGL rendering that includes video playback. I am pretty sure it's overkill for what you are trying to do.