views:

455

answers:

3

I'm interested in using Core Animation to drive custom animations in my OpenGL scene on the iPhone. I was hoping to find a mechanism that allowed you to generically animate a keypath of an NSObject or a delegate method for each animation tick. What I have come up with is to animate an arbitrary property of a CALayer and poll them from OpenGL to determine the progression of the animation. However, I'm convinced there's a better way to do it. What is the cleanest, safest way to tap into this great animation engine ?

A: 

If you want to animate view itself try to read http://stackoverflow.com/questions/630265/iphone-uiview-animation-best-practice. For animation inside OpenGL view it's better to use animated models or custom code but not Core Animation.

Pavel
What exactly is the matter with using Core Animation indirectly though? I'm aware of the strong advice against mixing Core Animation and OpenGL ON the screen but what if the CALayer is never added to a view?
limerick
Maybe I misunderstand something but in case you render your scene with OpenGL ES there is no need to use Core Animation at all. Look at this simple example by Apple:http://developer.apple.com/iphone/library/samplecode/GLES2Sample/index.htmlThey have UIView without any CALayer.
Pavel
I'm talking about using the animation capabilities of Core Animation to animate something in OpenGL. The CALayer I'd be using would not live on the screen. Instead it would be animated so that I could look up where the animation is supposed to be (according to the CALayer) and draw an OpenGL object on the screen in relation. In this way I would get a lot a lot for free, instead of writing my own animation engine.
limerick
A: 

This is possible, to use Core Animation to drive whatever you wish. I am sure it can be used for sound, too! (Given a little subclassing of CALayer). UIKit and Core Animation can be mixed quite freely, with a little care, as there is CAEAGLLayer for us to play with as well.

The key here is -(id)presentationLayer and -(id)modelLayer for using pure/only OpenGL; of course even the hierarchy can be used for your OpenGL scene too. But, using CALayer for rendering as well gets even more "for free", if this is possible for your circumstances (but also see CATransformLayer), if this is what you seek! =)

humasect
A: 

I suspect what you really want are just the easing functions used by core animation. These are easy enough to find with a little googling. Here's one example:

GLfloat bounceEaseOut(GLfloat ratio) {
    if (ratio < 1.0f/2.75f) {
        return 7.5625f*ratio*ratio;
    } else if (ratio < 2.0f/2.75f) {
        return 7.5625f*(ratio-=1.5f/2.75f)*ratio+0.75f;
    } else if (ratio < 2.5f/2.75f) {
        return 7.5625f*(ratio-=2.25f/2.75f)*ratio+0.9375f;
    } else {
        return 7.5625f*(ratio-=2.625f/2.75f)*ratio+0.984375f;
    }
}

You pass in how far into your tween you are (elapsedTime/tweenDuration) and it tells you what percent of the way through the tween's "timeline" you should be.