Skip to content

Instantly share code, notes, and snippets.

@bannzai
Last active January 16, 2024 14:32
Show Gist options
  • Save bannzai/07672828cdfe8d48180030fc38e307e7 to your computer and use it in GitHub Desktop.
Save bannzai/07672828cdfe8d48180030fc38e307e7 to your computer and use it in GitHub Desktop.
CarouselView.swift
import SwiftUI
struct Item: Identifiable {
let id = UUID()
var text = ""
var color: Color = .clear
}
struct CarouselView: View {
var items: [Item] = [
.init(text: "one", color: .red),
.init(text: "two", color: .blue),
.init(text: "three", color: .green),
.init(text: "four", color: .yellow)
]
var body: some View {
ScrollView(.horizontal) {
LazyHStack {
ForEach(items) { item in
ZStack {
RoundedRectangle(cornerRadius: 20)
.fill(item.color.gradient)
.frame(maxWidth: .infinity)
.frame(height: 120)
Text(item.text)
.font(.title)
.fontWeight(.bold)
}
.containerRelativeFrame(.horizontal)
}
}
.scrollTargetLayout()
}
.scrollTargetBehavior(.paging)
.safeAreaPadding(.horizontal, 16.0)
}
}
#Preview {
CarouselView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment