Skip to content

Instantly share code, notes, and snippets.

@bobergj
Last active May 20, 2025 08:43
Show Gist options
  • Save bobergj/12ae71bb374c45ed810b102ed34eaee7 to your computer and use it in GitHub Desktop.
Save bobergj/12ae71bb374c45ed810b102ed34eaee7 to your computer and use it in GitHub Desktop.
CarouselView
import AnySubviews
public import SwiftUI
public struct CarouselView<Content: View>: View {
let content: Content
public init(@ViewBuilder content: () -> Content) {
self.content = content()
}
public var body: some View {
ScrollView(.horizontal) {
LazyHStack(spacing: 8) {
BackportGroup(subviews: content) { subviews in
ForEach(subviews) { subview in
subview
.containerRelativeFrame(.horizontal)
.scrollTransition { content, phase in
content
.scaleEffect(y: phase.isIdentity ? 1 : 0.95)
}
}
}
}
.scrollTargetLayout()
}
.scrollTargetBehavior(.viewAligned(limitBehavior: .always))
.scrollClipDisabled(true)
.scrollIndicators(.hidden)
.shadow(color: Color(white: 0.0, opacity: 0.3), radius: 20.0, x: 0.0, y: 0.0)
.frame(height: 200)
}
}
#Preview {
CarouselView {
Color.red
Color.green
Color.yellow
Color.blue
}
.contentMargins(.horizontal, 16, for: .scrollContent)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment