9

What is a CALayer (as seen in the layer property on a UIView) and where would we use such a class?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
KingofHeaven
  • 1,195
  • 3
  • 14
  • 27
  • 1
    See also [What are Layers good for? What could I do with adding an Layer, and why should I think about Layers?](http://stackoverflow.com/questions/745096/what-are-layers-good-for-what-could-i-do-with-adding-an-layer-and-why-should-i), [When to use CALayer on the Mac/iPhone?](http://stackoverflow.com/questions/1447598/when-to-use-calayer-on-the-mac-iphone), and [Purpose of CALayer?](http://stackoverflow.com/questions/2962768/purpose-of-calayer) – Brad Larson Sep 04 '10 at 13:58

3 Answers3

21

A UIView is a aggregate class. It contains "stuff" for the event responder chain, stuff for handling the view hierarchy, etc., as well as stuff regarding what to draw on the display. The CALayer of a UIView is just the stuff regarding what to draw: the image bits, the scale, transform, animation properties, etc.

The Cocoa Touch UI is drawn by compositing layers... views on top of views on top of a window. 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.)

If you want to do something special with what a view draws or displays that isn't provided in the stock UIView class methods, it might be possible to do that special something by going directly to the CALayer: maybe swapping layers between views and/or images, drawing stuff off-screen, custom animations, etc.

There's lots more explained in Apple CALayer Class Reference document

Xavier Lowmiller
  • 1,381
  • 1
  • 15
  • 24
hotpaw2
  • 70,107
  • 14
  • 90
  • 153
0

UIView is built upon CALayers.That is simply a classes which is a visual content for UIViews. Just print UIView using NSLog and check, we could see its content layer and frame. Use Core Graphics to directly to work with CALayers rather UIView is UIKit element. self.view.layer.backgroundColor = [UIColor redColor].CGColor;

Gobi M
  • 3,243
  • 5
  • 32
  • 47
0

iOS Core Animation Layer (CALayer, Layer)

UIView is a first entry point which wraps a necessary functionality(e.g. handle touch event[About], rendering...) and expose mo low level API. CALayer in this case is responsible for a graphic/visualisation stuff - rendering, layout, animation

OpenGL
UIKit -> Core Animation -> Core Graphics -> Quartz -> Hardware

UIView has frame, bounds, and center which are actyally exposes CALeyr - frame, bounds, and position[About]

UIView contains root CALayer which can contains subleyers

let layer = CALayer()
layer.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 50, height: 50))
layer.backgroundColor = UIColor.magenta.cgColor
view1.layer.addSublayer(layer)

You can find a lot of cases where CALayer is used: animations, corner radius, shadow...

You are able to use CALayer for more low level stuff than UIView has

You are able to give a layer's name ho manage your layers

view.layer.name = "some name"

Please note that it's some extra work with layers, for example when you change appearance (light, dark)

doc

When the user changes the system appearance, the system automatically asks each window and view to redraw itself.

Debug layers from Xcode v11.4

Debug View Hierarchy -> Editor -> Show Layers

[iOS masksToBounds]

[iOS shadow]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205