iOS: Should I Add UIViews or CALayers for animation?
Asked Answered
T

4

7

Let's say I want to add 50 images to a view for the purpose of animating them. And let's suppose I'm planning on using Core Animation (e.g., CABasicAnimation) rather than "UIView" animation.

Am I better off implementing this by adding 50 subviews or 50 sublayers? Does it make a difference?

Thanks.

Tailorbird answered 14/1, 2011 at 0:25 Comment(0)
M
17

As I describe here, I've used both UIViews and CALayers in animations and found a negligible performance difference between them. UIViews are very lightweight wrappers around the layers. Also, any layer-based animations you need can be applied to a UIView's backing layer easily.

I've used CALayers directly in situations where I wanted to create cross-platform (Mac / iOS) UI elements, because CALayers are almost identical in their implementation on both platforms (unlike the significantly different NSViews and UIViews). CALayers don't have any touch-handling routines out of the box, but you can add that capability if you need to.

There are also some edge cases where you might want to work directly with layers, like when trying to do limited 3-D manipulation of the layers (as in a CoverFlow effect) or when using a CAReplicatorLayer to produce particle effects.

Maines answered 14/1, 2011 at 3:22 Comment(2)
I ended up going with UIViews, primarily because UIView animation with blocks is so much easier to implement in cases where it's important to run animations/take actions successively. (Those completion blocks are very clean, and using a delegate 'animationDidStop' call was going to be a nightmare to implement in my particular case.)Tailorbird
@Greg - Yes, the new block-based UIView animations are pretty clean. However, you can get similar block completion callbacks with CAAnimations using CATransaction's +setCompletionBlock:, which will execute a block on completion of all animations wrapped within that transaction. Just something to think about if you might want to go that way.Maines
S
1

UIViews contain sublayers, so they are heavier weight, and contain stuff you probably don't need for all 50 images, such as event and touch handlers/variables. So using layers would probably be slightly more efficient and use a bit less memory than using views for each image.

Spina answered 14/1, 2011 at 0:38 Comment(0)
S
1

The difference for such a small number of images is negligible. Use what's most convenient.

Seductive answered 14/1, 2011 at 0:41 Comment(2)
In my experimentation, I found that about 50 images is what I could animate before I got some stuttering on an iPhone 4. (I can only assume this number varies greatly depending upon content/size of images, number of translucent layers below the animated images, etc.)Tailorbird
And the phone model :DMetrify
E
0

I've not done animation (yet :-), but the stuff I remember reading about it suggests to create one image with all 50 tiled on it and then just offset to the correct image when drawing. That way you only need one layer or UIImage or whatever to display it. I don't know about speed, but I'd guess it would save memory and would probably be easier to manage and code.

Echelon answered 14/1, 2011 at 0:44 Comment(1)
That's generally referred to as a "sprite" and applies to OpenGL animations but doesn't really apply to CoreAnimation.Seductive

© 2022 - 2024 — McMap. All rights reserved.