Search code examples
iphoneperformancecore-animation

in which situations does it make sense to create only an lighteight CALayer rather than a fat UIView?


The only thing that comes in my mind is displaying simple images. But that wouldn't make sense if you want to animate them, right? So is it any useful to make a CALayer only instead of an UIView that comes along with a whole bunch of them (all those trees...)?


Solution

  • You use CALayer when you are not getting any benefits from using UIView and when it's not possible to do the thing you want to do with UIView.

    Following this line of thought, if your need is not making use of any UI controls and/or you want to create your own custom animation (2D or 2.5D animation), it makes sense to drop down to the CoreAnimation level and create your own CALayer to do your thing there.

    If however you are only interested in performing basic animation and it can be done through UIView, the API is usually simpler, so it will make sense to use UIView to save yourself time there.