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.

Suhaib
  • 2,031
  • 3
  • 24
  • 36
Greg Maletic
  • 6,225
  • 8
  • 54
  • 73

4 Answers4

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.

Community
  • 1
  • 1
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • 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.) – Greg Maletic Jan 27 '11 at 20:08
  • 1
    @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. – Brad Larson Jan 27 '11 at 20:39
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.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
1

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

Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
  • 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.) – Greg Maletic Jan 27 '11 at 20:06
  • And the phone model :D – BallpointBen Feb 25 '17 at 03:58
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.

drekka
  • 20,957
  • 14
  • 79
  • 135