Swift Academy · Lesson

CALayer Fundamentals

Work with layers, their properties, and hierarchy.

Lesson 1 of 413 steps

CALayer Fundamentals is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

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 = true

Geometry: 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 pivot

Visual 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.cgColor

Shadows

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 = 6

Sublayers

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.cgColor

Gradient 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.scale

Layers 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.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “CALayer Fundamentals” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “CALayer Fundamentals”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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.

All lessons in this course

  1. CALayer Fundamentals
  2. Implicit and Explicit Animations
  3. Transforms and 3D Effects
  4. Custom Drawing with Core Graphics
← Back to Swift Academy