views:

3208

answers:

5

I'm struggling with conceptualizing animations with a CALayer as opposed to UIView's own animation methods. Throw "Core Animation" into this and, well, maybe someone can articulate these concepts from a high level so I can better visualize what's happening and why I'd want to migrate UIView animations (which I'm quite familiar with now) to CALayer animations on the iPhone. Every view in Cocoa-Touch automatically gets a layer. And, it seems, you can animate one and/or the other?!? Even mix them together?!? But why? Where's the line? What's the pro/con to each?

The Core Animation Programming Guide immediately jumps into Layer & Timing Classes and I think need to take a step back and understand why these varied pieces exist and how relate to each other.

+2  A: 

UIView transforms are only 2D and are restricted to that, LAyer transforms however can be 3D and you should use those if you want to do 3D stuff, UIView animation will work if you change either the UIView transform or the CALayer transform. So at a basic level, you can do a lot more manipulation when you are working with a Layer rather than the View.

Daniel
+6  A: 

An UIView is always rendered to a CALayer. When you use UIView methods to animate a view, you are effectively manipulating the underlying CALayer.

If you need to do simple things, use the UIView methods. For more complex situatins, or if you want layers not associated with any view in particular, use CALayers.

Marco Mustapic
+8  A: 

I've done a bunch of apps in the past year. Here's my rule of thumb:

  1. Use UIView until it doesn't do what you want.
  2. Then move to CoreAnimation. But before you get into it too much...
  3. If you write more than a few animations, use Cocos2D.
Chris Garrett
What's Cocos2D doing better? Don't you have other problems then, regarding the touch event handling and many other stuff that misses in openGL ES?
Thanks
Cocos2D adds a lot of convenience methods, so code is a lot more compact. They have some great built-in effects. In a nutshell, if you are doing a bunch of animation, you will write less code than either OpenGL ES or Core Animation if you use Cocos2D. They have their own touch handling methods, so that's not an issue at all.
Chris Garrett
My suggestion is not to use Cocos2D unless you are writing a game. I think that is really its best use-case. I agree, though, that you should use UIView animations for as long as you can, and for anything beyond that use CoreAnimation.
Jonathan Sterling
+8  A: 

Use views for control and layers for eye candy. Layers don't receive events so it's easier to use a view for those cases, but when you want to animate a sprite or backgrounds, etc., layers make sense. Events pass right through layers to the backing view so you can have a pretty visual representation without messing up your events. Try to overlay a view that you're just using for visual representation and you'll have to pass tap events through to the underlying view yourself.

Matt Long
That helped, thanks :)
Spanky
+2  A: 

I am not sure if I am misunderstanding Chris' response to "What's Cocos2D doing better? Don't you have other problems then, regarding the touch event handling and many other stuff that misses in openGL ES?"

It sounds like the answer suggests Cocos2D is not based on the OpenGL ES framework when in fact it actual is. While it is a great 2D game engine it does implement OpenGL for much of it's rendering - attached to a physics library it allows for a lot of very interesting possibilities for animation - and Chris is correct - it is a lot less coding indeed.

TouchGameDev