Swift Academy · Lesson

Transforms and 3D Effects

Apply affine and 3D transforms.

Lesson 3 of 413 steps

Transforms and 3D Effects is a free Swift Academy lesson on CoddyKit. This is lesson 3 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.

Transforms Move and Distort

A transform reshapes a layer—translate, scale, rotate, skew—without changing its bounds. Core Animation supports both 2D (CGAffineTransform) and 3D (CATransform3D).

2D Affine Transforms

CGAffineTransform handles flat transforms. Combine them by concatenation; order matters.

import UIKit
var t = CGAffineTransform.identity
t = t.scaledBy(x: 1.5, y: 1.5)
t = t.rotated(by: .pi / 6)
layer.setAffineTransform(t)

CATransform3D

CATransform3D is a 4x4 matrix enabling true 3D. Start from CATransform3DIdentity and apply translation, scale, and rotation in space.

var t = CATransform3DIdentity
t = CATransform3DScale(t, 1.2, 1.2, 1)
layer.transform = t

3D Rotation

Rotate around any axis with CATransform3DRotate(t, angle, x, y, z). Rotating around the y-axis flips a card; around x tilts it forward.

let angle = CGFloat.pi / 4
layer.transform = CATransform3DRotate(
    CATransform3DIdentity, angle, 0, 1, 0) // around y

Perspective

Plain 3D rotation looks flat without perspective. Set element m34 of the identity matrix to a small negative value (about -1/500) to add depth.

var perspective = CATransform3DIdentity
perspective.m34 = -1.0 / 500.0
layer.transform = CATransform3DRotate(perspective, .pi / 4, 0, 1, 0)

Why m34 Creates Depth

The m34 term divides x and y by depth, so farther points shrink. Smaller magnitudes (larger denominator) give subtle perspective; larger ones exaggerate it.

// -1/2000 = subtle, -1/200 = dramatic
perspective.m34 = -1.0 / 1000.0

Anchor Point and Transforms

Transforms pivot around the anchorPoint. To rotate a card around its left edge, set anchorPoint to (0, 0.5) first.

layer.anchorPoint = CGPoint(x: 0, y: 0.5)
layer.transform = CATransform3DRotate(
    CATransform3DIdentity, .pi / 3, 0, 1, 0)

Animating Transforms

The whole transform is animatable. Animate the transform key path, or sub-keys like transform.rotation.y for a single axis.

let spin = CABasicAnimation(keyPath: "transform.rotation.y")
spin.fromValue = 0
spin.toValue = CGFloat.pi * 2
spin.duration = 1.5
layer.add(spin, forKey: "spin")

Sublayer Transforms

Set sublayerTransform on a parent to apply perspective to all its children at once—handy for 3D containers and carousels.

var t = CATransform3DIdentity
t.m34 = -1.0 / 500.0
parentLayer.sublayerTransform = t

zPosition and Depth Sorting

zPosition controls draw order in 3D space. Higher values render in front. Combine with rotation for layered, overlapping 3D scenes.

frontCard.zPosition = 10
backCard.zPosition = 0

Combining Transforms

Concatenate transforms to compose effects. Remember matrix multiplication is not commutative—rotate-then-scale differs from scale-then-rotate.

var t = CATransform3DIdentity
t = CATransform3DRotate(t, .pi / 6, 0, 0, 1)
t = CATransform3DScale(t, 2, 2, 1)

Quick Check: Transforms

Test your understanding of 3D transforms.

Recap: Transforms and 3D Effects

Transforms reshape layers without altering bounds: CGAffineTransform for 2D, CATransform3D for true 3D. Add depth by setting m34 (about -1/500), pivot via anchorPoint, and apply container perspective with sublayerTransform.

Animate transforms whole or per-axis, sort depth with zPosition, and remember transform composition order matters.

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 “Transforms and 3D Effects” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Transforms and 3D Effects”, 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 “Transforms and 3D Effects”?

Apply affine and 3D transforms. 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 3 of 4.

How long does the “Transforms and 3D Effects” 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