In my iOS app, I do a ton of trigonometric calculations based on a given point (specified by CGPoint) and then create some transformation matrices based on those calculations to finally be used in an OpenGL drawing (via GLKit). I’d like to create an animation by changing that fundamental CGPoint over time, but I’m not sure what approach I should use for the animation.
What I’m really looking for is an API that allows me to specify a function to be called on each iteration, much like NSTimer does, but it’d be really cool if I could take advantage ease in/out, etc. The only piece of data that needs to be modified each iteration is my main CGPoint, and the rest of the rendering can be determined from that.
Approaches I’ve considered, but abandoned:
-
Core Animation: I’m using OpenGL to draw, so Core Animation doesn’t seem to help.
-
NSTimer: This doesn’t give me the flexibility of bezier curves and seems very manual. -
Heartbeat based on a given framerate: I only need to re-render when the point changes, and most of the time it is stationary. Doesn’t feel like a heartbeat is the right approach.
Does something exist like what I’m describing? Do I have to write it myself? Or am I just misunderstanding the tools provided for me which suggests I should take another look at how I’m drawing my graphics?
I agree with the other poster. Assuming you can use iOS 5, you should use GLKView and GLKViewController. That’s set up to call you once per screen refresh (using a CADisplayLink internally .) If you don’t want to be iOS 5 only, you can set up a CADisplayLink yourself.
Core Animation isn’t useful for OpenGL rendering. However, you can use the design of Core Animation to drive your design. Core Animation (like the rest of Cocoa) is build on top of OpenGL, so you can do everything CA does yourself. It just takes work. (sometimes a LOT of work.)
Core Animation use a motion-based, not a frame based, animation model. Each time it renders the scene, it decides how much motion should be applied based on the elapsed time since the beginning of the animation. If it gets behind in rendering frames, the next frame moves further, so the motion over time is consistent.
As far as ease in/ease out timing, you can do that yourself too. You’d need to read up on animation timing. It uses a non-linear mapping of input time to output time, using bezier curves to change the shape of the curve at the beginning and the end.