Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CALayer & drawRect

In View and Window Architecture is stated, quote:

Views work in conjunction with Core Animation layers to handle the rendering and animating of a view’s content. Every view in UIKit is backed by a layer object (usually an instance of the CALayer class), which manages the backing store for the view and handles view-related animations.

Farther in the "The View Drawing Cycle" section is stated:

The UIView class uses an on-demand drawing model for presenting content. When a view first appears on the screen, the system asks it to draw its content. The system captures a snapshot of this content and uses that snapshot as the view’s visual representation.

Does that mean, that the content drawn in a view in its drawRect method call, is captured in a snapshot in saved in its backing core animation layer?

If not, where do this content snapshot "reside"?

If not, does that mean that CALayer is used to render "static" content, content that doesn't change very often, and drawRect is used to render content that changes often, for example in a game app?

p.s.

The questions are not related to any particular code implementation.

I just want to understand the ios view-layer architecture.

like image 991
Stefan Fachmann Avatar asked Apr 12 '13 08:04

Stefan Fachmann


People also ask

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.

What is the layer property of 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.

How do I add CALayer to view?

Your implementation of this method can do any of the following: - Adjust the size and position of any immediate subviews. - Add or remove subviews or Core Animation layers. - Force a subview to be redrawn by calling its setNeedsDisplay or setNeedsDisplayInRect: method.

What is view layer Swift?

Layers: All UIView objects have a "backing layer". Under the covers, UIKit uses the CoreAnimation framework to draw the view to the screen, and the main object in Core Animation that draws to the screen is a CALayer .


1 Answers

Does that mean, that the content drawn in a view in its drawRect method call, is captured in a snapshot in saved in its backing core animation layer?

Yes. Everything uses layers under the hood. UIView's -drawRect will capture what you draw and (as far as I know) set the content on a sublayer of the view. It might even do this on the main layer object. That's where the 'snapshot' is saved.

If not, does that mean that CALayer is used to render "static" content, content that doesn't change very often, and drawRect is used to render content that changes often, for example in a game app?

How often the content changes doesn't really affect the choice. There is not much difference in using drawRect vs. manually creating CALayers. It depends on how you want to organize the sub-elements in your views, or if you want to create low level reusable layer objects without the details of UIView (e.g. CATextLayer). If you have various different sub-elements to draw then you may split them into different layers with their own custom drawing code. If you just have one simple piece of content to draw, you can do that all in a single drawRect implementation.

Having said this, you do need to be aware that each layer will end up being a separate GPU "element", so there can be performance benefits to reducing the number of layers you have, or using the shouldRasterize+rasterizationScale properties of a parent layer. This will take a snapshot of an entire layer hierarchy and create a single rasterized image to be rendered instead of n separate ones.

like image 149
Mike Weller Avatar answered Oct 15 '22 04:10

Mike Weller