How do I build a horizontal, swipeable, paginated carousel in SwiftUI?

Generated on 7/30/2024

1 search

Tailscan

This post is brought to you by Tailscan, the best devtool for Tailwind CSS.

To build a horizontal, swipeable, paginated carousel in SwiftUI, you can use a combination of ScrollView and the paging behavior. Here's a basic example to get you started:

import SwiftUI

struct CarouselView: View {
    let items = Array(1...10).map { "Item \($0)" }
    
    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 20) {
                ForEach(items, id: \.self) { item in
                    Text(item)
                        .frame(width: 200, height: 200)
                        .background(Color.blue)
                        .cornerRadius(10)
                        .shadow(radius: 5)
                }
            }
            .padding()
        }
        .pagingEnabled(true)
    }
}

struct ContentView: View {
    var body: some View {
        CarouselView()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

In this example, ScrollView is used to create a horizontal scrollable view, and the pagingEnabled(true) modifier is applied to enable paging behavior, which makes the scroll view snap to the nearest item.

For more advanced customizations, such as creating unique scroll effects or transitions, you can refer to the session Create custom visual effects with SwiftUI. This session covers how to use the scrollTransition modifier to create custom scroll effects, such as rotating and offsetting items based on their position in the scroll view.

For example, to create a circular carousel effect, you can use the scrollTransition modifier:

ScrollView(.horizontal, showsIndicators: false) {
    HStack(spacing: 20) {
        ForEach(items, id: \.self) { item in
            Text(item)
                .frame(width: 200, height: 200)
                .background(Color.blue)
                .cornerRadius(10)
                .shadow(radius: 5)
                .scrollTransition { content, phase in
                    content
                        .rotationEffect(.degrees(phase.value * 360))
                        .offset(x: phase.value * 100)
                }
        }
    }
    .padding()
}
.pagingEnabled(true)

This code snippet demonstrates how to apply a rotation and offset effect to each item in the scroll view based on its position.

For more detailed information and examples, you can watch the session Create custom visual effects with SwiftUI.

SwiftUI essentials

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

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.

Create custom visual effects with SwiftUI

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.

Demystify SwiftUI containers

Demystify SwiftUI containers

Learn about the capabilities of SwiftUI container views and build a mental model for how subviews are managed by their containers. Leverage new APIs to build your own custom containers, create modifiers to customize container content, and give your containers that extra polish that helps your apps stand out.

Migrate your TVML app to SwiftUI

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.