How do I do a carrousel in SwiftUI?
Generated on 7/27/2024
1 search

This post is brought to you by Tailscan,
the best devtool for Tailwind CSS.
To create a carousel effect in SwiftUI, you can use the scrollTransition
modifier to transform a standard collection of elements into a custom scroll view. Here's a brief overview of how you can achieve this:
-
Use a Horizontal Scroll View: Start with a horizontal scroll view containing your items, such as photos.
-
Apply the
scrollTransition
Modifier: This modifier allows you to change the rotation and offset of each item based on its position in the scroll view. As you scroll, items on the leading and trailing edges can be rotated to create a circular carousel effect. -
Adjust Rotation and Offset: Use the
value
property to determine how far off-screen an item is and apply rotation accordingly. If an item is fully on-screen, theisIdentity
property will be true.
Here is a simplified example based on the context provided:
import SwiftUI
struct CarouselView: View {
let photos: [String] // Array of photo names
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(photos, id: \.self) { photo in
Image(photo)
.resizable()
.scaledToFit()
.frame(width: 200, height: 200)
.scrollTransition { content, phase in
content
.rotation3DEffect(
.degrees(phase.isIdentity ? 0 : -30),
axis: (x: 0, y: 1, z: 0)
)
.offset(x: phase.isIdentity ? 0 : -50)
}
}
}
}
}
}
For more detailed information and examples, you can refer to the session Create custom visual effects with SwiftUI at WWDC 2024. This session covers how to use the scrollTransition
modifier to create unique scroll effects, including a carousel.

SwiftUI essentials
Join us on a tour of SwiftUI, Apple’s declarative user interface framework. Learn essential concepts for building apps in SwiftUI, like views, state variables, and layout. Discover the breadth of APIs for building fully featured experiences and crafting unique custom components. Whether you’re brand new to SwiftUI or an experienced developer, you’ll learn how to take advantage of what SwiftUI has to offer when building great apps.

Enhance your UI animations and transitions
Explore how to adopt the zoom transition in navigation and presentations to increase the sense of continuity in your app, and learn how to animate UIKit views with SwiftUI animations to make it easier to build animations that feel continuous.

Migrate your TVML app to SwiftUI
SwiftUI helps you build great apps on all Apple platforms and is the preferred toolkit for bringing your content into the living room with tvOS 18. Learn how to use SwiftUI to create familiar layouts and controls from TVMLKit, and get tips and best practices.

Create custom visual effects with SwiftUI
Discover how to create stunning visual effects in SwiftUI. Learn to build unique scroll effects, rich color treatments, and custom transitions. We’ll also explore advanced graphic effects using Metal shaders and custom text rendering.

What’s new in SwiftUI
Learn how you can use SwiftUI to build great apps for any Apple platform. Explore a fresh new look and feel for tabs and documents on iPadOS. Improve your window management with new windowing APIs, and gain more control over immersive spaces and volumes in your visionOS apps. We’ll also take you through other exciting refinements that help you make expressive charts, customize and layout text, and so much more.