Last active
October 21, 2024 08:34
-
-
Save bannzai/07672828cdfe8d48180030fc38e307e7 to your computer and use it in GitHub Desktop.
CarouselView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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