What is a CALayer (as seen in the layer
property on a UIView) and where would we use such a class?
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
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;
iOS Core Animation Layer (CALayer, Layer)
CALayer
is responsible for a graphic/visualisation stuff - Basis for drawing(e.g. content, corner radius, shadow[About], transforms ...) and animations. It can contains sublayers which create CALayer hierarchy
A layer merely manages the state information surrounding a bitmap
CALayer
is used in lot of cases:
UIView
adds extra functionality(e.g. handle touch event[About], rendering...) and exposes access toCALayer
inside.- video processing
- ...
OpenGL
UIKit -> Core Animation -> Core Graphics -> Quartz -> Hardware
UIView
has frame
, bounds
, and center
which are actually exposes CALayer
- frame
, bounds
, and position
[About]
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 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)
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 Bounds vs Frame]
[iOS CALayer anchorPoint and position]
[iOS shadow and cornerRadius]
[iOS CALayer.masksToBounds]
[iOS CALayer.mask]
[iOS alpha vs opacity vs opaque]
[iOS CALayer.shouldRasterize]
© 2022 - 2024 — McMap. All rights reserved.