CALayer Fundamentals
Work with layers, their properties, and hierarchy.
CALayer Fundamentals is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a CALayer?
CALayer is the backing store for everything you see in UIKit. Each UIView is drawn by a layer that holds its visual content as a texture on the GPU.
Working with layers directly unlocks hardware-accelerated graphics and animation.
import UIKit
let layer = CALayer()Layers Behind Views
Every view has a layer property. Setting layer properties is often simpler and cheaper than custom drawing.
view.layer.cornerRadius = 12
view.layer.masksToBounds = trueGeometry: frame, bounds, position
A layer has bounds (its own coordinate space), position (where its anchor sits in the parent), and a derived frame. Editing any one updates the others.
let layer = CALayer()
layer.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
layer.position = CGPoint(x: 150, y: 150)The Anchor Point
anchorPoint (default 0.5, 0.5 = center) defines the layer’s origin for positioning and transforms. Rotations and scales pivot around it.
layer.anchorPoint = CGPoint(x: 0, y: 0) // top-left pivotVisual Properties
Layers expose visual styling directly: backgroundColor (a CGColor), cornerRadius, borderWidth, borderColor, and opacity.
layer.backgroundColor = UIColor.systemBlue.cgColor
layer.cornerRadius = 8
layer.borderWidth = 2
layer.borderColor = UIColor.white.cgColorShadows
Layers draw shadows with shadowColor, shadowOpacity, shadowOffset, and shadowRadius. Set a shadowPath for performance.
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.3
layer.shadowOffset = CGSize(width: 0, height: 4)
layer.shadowRadius = 6Sublayers
Layers form a tree. Add children with addSublayer; they are positioned in the parent’s coordinate space.
let parent = CALayer()
let child = CALayer()
child.frame = CGRect(x: 10, y: 10, width: 40, height: 40)
parent.addSublayer(child)Specialized Layer Types
UIKit ships layer subclasses: CAShapeLayer (vector paths), CAGradientLayer (gradients), CATextLayer (text), and CAReplicatorLayer (duplicated children).
let shape = CAShapeLayer()
shape.path = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 50, height: 50)).cgPath
shape.fillColor = UIColor.systemRed.cgColorGradient Layers
CAGradientLayer renders smooth color blends defined by colors, locations, startPoint, and endPoint.
let gradient = CAGradientLayer()
gradient.colors = [UIColor.systemBlue.cgColor, UIColor.systemPurple.cgColor]
gradient.startPoint = CGPoint(x: 0, y: 0)
gradient.endPoint = CGPoint(x: 1, y: 1)contentsScale and Sharpness
Set contentsScale to the screen scale so layer-drawn content is crisp on Retina displays. Forgetting it produces blurry text and shapes.
layer.contentsScale = UIScreen.main.scaleLayers Are Lightweight
Layers do not handle touch events or Auto Layout; they are pure visual objects. That makes them cheaper than views for decorative or animated content.
Quick Check: CALayer
Test your understanding of CALayer fundamentals.
Recap: CALayer Fundamentals
CALayer backs every UIKit view and offers GPU-accelerated visuals: bounds/position/anchorPoint geometry, styling (corner radius, border, shadows), and a tree of sublayers.
Specialized subclasses handle shapes, gradients, text, and replication. Set contentsScale for sharpness, and remember layers are lightweight visual objects with no touch or Auto Layout. This foundation powers the animations to come.
Frequently asked questions
Is the “CALayer Fundamentals” lesson free?
Yes — the full text of “CALayer Fundamentals” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “CALayer Fundamentals”?
Work with layers, their properties, and hierarchy. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “CALayer Fundamentals” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.