Transforms and 3D Effects
Apply affine and 3D transforms.
Transforms and 3D Effects is a free Swift Academy lesson on CoddyKit — lesson 3 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.
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 = t3D 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 yPerspective
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.0Anchor 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 = tzPosition 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 = 0Combining 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.
Frequently asked questions
Is the “Transforms and 3D Effects” lesson free?
Yes — the full text of “Transforms and 3D Effects” 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 “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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
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
- CALayer Fundamentals
- Implicit and Explicit Animations
- Transforms and 3D Effects
- Custom Drawing with Core Graphics