Creating 3D Perspective Effects with SwiftUI’s ProjectionEffect

Durul Dalkanat
2 min readMay 23, 2024

One of the most powerful features of SwiftUI is its ability to apply visual effects to views in a concise and declarative way. While many of these effects are common ones like shadows or blurs, SwiftUI also allows us to leverage lower-level graphics functionality like geometric transformations.

In this article, we’ll explore how to use SwiftUI ProjectionEffect to apply a 3D perspective transform to a view. This effect makes the view appear skewed or tilted, creating an illusion of depth.

Let's dive into the code:

struct ContentView: View {
let transform: CGAffineTransform = {
var transform = CGAffineTransform.identity
transform.c = tan(0.7)
return transform
}()

var body: some View {
GeometryReader { geometry in
VStack {
Image(systemName: "apple.terminal")
.font(.system(size: 200))
.foregroundStyle(
LinearGradient(
colors: [.yellow, .red],
startPoint: .topTrailing,
endPoint: .bottomLeading
)
)
.frame(width: geometry.size.width / 4, height: geometry.size.height / 2)
.projectionEffect(ProjectionTransform(transform))
.shadow(color: .gray, radius: 10, x: 5, y: 50)
}
}
}
}

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

No responses yet

What are your thoughts?