Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use CALayer on the Mac/iPhone?

Tags:

I'm slightly confused when to use CALayer on the iPhone or Mac and when not to use it? CoreAnimation works just fine on my UIView based objects without having to use CALayer. When is the appropriate time to dig into this class?

like image 404
randombits Avatar asked Sep 19 '09 02:09

randombits


People also ask

What is the difference between UIView and CALayer?

Working with UIViews happens on the main thread, it means it is using CPU power. CALayer: Layers on other hand have simpler hierarchy. That means they are faster to resolve and quicker to draw on the screen. There is no responder chain overhead unlike with views.

What is a CALayer?

A CALayer is a layer in the composition stack which goes on top of some layers, and potentially underneath other layers. (e.g. an image in a button in a table cell in a view in a split view, etc.)

Are often used to provide the backing store for views but can also be used without a view to display content?

Overview. Layers are often used to provide the backing store for views but can also be used without a view to display content. A layer's main job is to manage the visual content that you provide but the layer itself has visual attributes that can be set, such as a background color, border, and shadow.

What is Layer property on a UIView object?

All UIView subclasses have a layer property, which is responsible for drawing their contents efficiently. These layers are powered by Core Animation, which handles all the drawing and animation that UIKit requests.


2 Answers

In my benchmarks, UIView and CALayer provide about the same level of performance on the iPhone. As rpetrich mentions in his comment, UIViews are a thin wrapper around CALayers. On the Mac, CALayers are much more lightweight than NSViews.

As Ben points out, you can go beyond the capabilities of implicit animations by working directly with CALayers, even providing some 3-D effects through CATransform3D. In many cases, you can do this even with your standard views by accessing the backing layer (if the view is layer-backed).

Another concern is cross-platform (Mac / iPhone) code. My iPhone application uses an all-CALayer interface for its primary view in large part because I can use the exact same code for drawing that interface in its Mac counterpart. For another example of this, I direct you to the Core Plot framework, which draws graphs entirely using CALayers and works on both Mac and iPhone. CALayers are pretty much the same on both platforms, where UIView and NSView have very different interfaces.

like image 138
Brad Larson Avatar answered Jan 01 '23 21:01

Brad Larson


If you can do what you want with 'implicit animation' (that offered by UIKit/AppKit without having to dig into CA,layers, and animators) then definitely go that route.

CoreAnimation comes into play when you start using more complex animations, such as non-linear motion, or repeating effects, and certain synchronized effects. There's a LOT you can do with it, but it is a pretty heavy duty tool (with a commensurate learning curve, at least compared to the UIKit stuff).

like image 22
Ben Gottlieb Avatar answered Jan 01 '23 20:01

Ben Gottlieb