Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Should I Add UIViews or CALayers for animation?

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.

like image 996
Greg Maletic Avatar asked Jan 14 '11 00:01

Greg Maletic


2 Answers

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.

like image 154
Brad Larson Avatar answered Sep 21 '22 18:09

Brad Larson


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.

like image 22
hotpaw2 Avatar answered Sep 22 '22 18:09

hotpaw2