UIView animation vs CALayers
Asked Answered
V

5

75

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.

Vilberg answered 16/7, 2009 at 19:20 Comment(2)
This was an excellent, general purpose question and all of the answers are excellent. I voted up everyone on this thread.Lauralauraceous
Great question. Does anyone know best practise for animating a UIView's CALayer? In my example I need a 3D effect, and I find animating the layer cause the 'frame' of the UIView to be misaligned with the layer (so for a button, the taps won't match up any more).Chinachinaberry
S
54

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.

Scandinavia answered 26/7, 2009 at 5:35 Comment(0)
M
26

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.

Marinna answered 16/7, 2009 at 20:1 Comment(0)
A
20

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.
Adductor answered 16/7, 2009 at 20:13 Comment(3)
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?Scouring
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.Adductor
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.Ricciardi
M
8

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.

Mitra answered 16/7, 2009 at 19:34 Comment(0)
U
1

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.

Unsteady answered 15/11, 2009 at 22:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.