Created
July 25, 2024 13:38
-
-
Save Codelaby/3d80c1e475bf036399d97ee8eb5861be to your computer and use it in GitHub Desktop.
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 | |
//RandomAccessCollection & MutableCollection & RangeReplaceableCollection & Equatable & Hashable | |
struct CoverFlowCarousel<Content: View, Data: RandomAccessCollection>: View where Data.Element: Identifiable { | |
@Environment(\.layoutDirection) var direction | |
private var config: Config | |
private let data: Data | |
@Binding var selection: Data.Element.ID? | |
private let content: (Data.Element) -> Content | |
init(data: Data, selection: Binding<Data.Element.ID?>, config: Config = Config(), @ViewBuilder content: @escaping (Data.Element) -> Content) { | |
self.data = data | |
self._selection = selection | |
self.config = config | |
self.content = content | |
} | |
struct Config { | |
var cornerRadius: CGFloat = 20 | |
var spacing: CGFloat = 16 | |
var cardWidth: CGFloat = 150 | |
var minimumCardWidth: CGFloat = 40 | |
var hasOpacity: Bool = true | |
var oppacityValue: CGFloat = 0.4 | |
var hasScale: Bool = true | |
var scaleValue: CGFloat = 0.2 | |
} | |
var body: some View { | |
GeometryReader { | |
let size = $0.size | |
ScrollView(.horizontal, showsIndicators: false) { | |
HStack(spacing: config.spacing) { | |
//ForEach(Array(data.enumerated()), id: \.element.id) { index, item in | |
ForEach(data, id: \.self.id) { item in | |
ItemView(item) | |
.id(item.id) | |
// .scrollTransition( | |
// topLeading: .identity, | |
// bottomTrailing: .interactive | |
// ) { view, transition in | |
// view.opacity(transition.value) | |
// } | |
//content(item) | |
// .scrollTransition( | |
// topLeading: .interactive, | |
// bottomTrailing: .interactive | |
// ) { view, transition in | |
// return view | |
// .opacity(transition.isIdentity ? 1 : 0.3) | |
// .scaleEffect(y: transition.isIdentity ? 1 : 0.8) | |
// | |
// } | |
//.containerRelativeFrame(.horizontal, count: 1, span: 1, spacing: 0) | |
//.containerRelativeFrame(.horizontal) { width, _ in width / 2 } // screen available height / 2 | |
//.clipShape(RoundedRectangle(cornerRadius: config.cornerRadius)) | |
} | |
} | |
.scrollTargetLayout() | |
} | |
.safeAreaPadding(.horizontal, max((size.width - config.cardWidth) / 2, 0)) | |
//.contentMargins(.horizontal, (size.width - config.cardWidth) / 2) | |
.scrollTargetBehavior(.viewAligned(limitBehavior: .always)) | |
.scrollPosition(id: $selection) | |
} | |
} | |
// @ViewBuilder | |
// private func ItemView(_ item: Data.Element) -> some View { | |
// GeometryReader { proxy in | |
// content(item) | |
// } | |
// .frame(width: config.cardWidth) | |
// .clipped() | |
// } | |
@ViewBuilder | |
private func ItemView(_ item: Data.Element) -> some View { | |
GeometryReader { proxy in | |
let size = proxy.size | |
let minX = proxy.frame(in: .scrollView(axis: .horizontal)).minX * (direction == .rightToLeft ? -1 : 1) | |
let progress = minX / (config.cardWidth + config.spacing) | |
let minimumCardWidth = config.minimumCardWidth | |
let diffWidth = config.cardWidth - minimumCardWidth | |
let reducingWidth = progress * diffWidth | |
/// Limiting diffWidth as the Max Value | |
let cappedWidth = min(reducingWidth, diffWidth) | |
let resizedFrameWidth = size.width - (minX > 0 ? cappedWidth : min(-cappedWidth, diffWidth) ) | |
let negativeProgress = max(-progress, 0) | |
let opacityValue = config.oppacityValue * abs(progress) | |
let scaleValue = config.scaleValue * abs(progress) | |
content(item) | |
.frame(width: size.width, height: size.height) | |
.frame(width: resizedFrameWidth) | |
.clipShape(RoundedRectangle(cornerRadius: config.cornerRadius)) | |
// .mask { | |
// let scaledHeight = (1 - scaleValue) * size.height | |
// RoundedRectangle(cornerRadius: config.cornerRadius) | |
// .frame(height: config.hasScale ? scaledHeight : size.height) | |
// } | |
.offset(x: -reducingWidth) | |
.offset(x: min(progress, 1) * diffWidth) | |
.offset(x: negativeProgress * diffWidth) | |
.opacity(config.hasOpacity ? 1 - opacityValue : 1) | |
.scaleEffect(y: config.hasScale ? 1 - scaleValue : 1) | |
} | |
.frame(width: config.cardWidth) | |
} | |
} | |
#Preview { | |
struct PreviewWrapper: View { | |
struct Landmark: Identifiable, Hashable, Equatable { | |
var id: UUID = .init() | |
var image: String | |
var name: String | |
} | |
let landmarks = [ | |
Landmark(image: "Hongcun", name: "A"), | |
Landmark(image: "Kaifeng", name: "B"), | |
Landmark(image: "fenghuang", name: "C"), | |
Landmark(image: "Jiangnan", name: "D"), | |
Landmark(image: "Enshi", name: "E"), | |
Landmark(image: "ghizhou", name: "F") | |
] | |
@State private var currentIndex: UUID? | |
@State private var selectedLandmark: Landmark? | |
var body: some View { | |
VStack { | |
Text("Cover Flow SwiftUI").font(.largeTitle).bold().multilineTextAlignment(.center) | |
Text("RTL support, adjust scale, opacity").font(.footnote).foregroundStyle(.secondary) | |
Spacer() | |
NavigationStack { | |
CoverFlowCarousel(data: landmarks, selection: $currentIndex, | |
config: .init( | |
cornerRadius: 20, | |
spacing: 16, | |
hasOpacity: false, | |
hasScale: false | |
) | |
) { landmark in | |
VStack { | |
Image(landmark.image) | |
.aspectRatio(1.1, contentMode: .fill) | |
.overlay { | |
Text(landmark.name) | |
.padding(8) | |
.background(.thinMaterial, in: .circle) | |
} | |
.onTapGesture { | |
selectedLandmark = landmark | |
} | |
} | |
} | |
.frame(maxHeight: 240) | |
.navigationTitle("CoverFlow") | |
.navigationDestination(isPresented: Binding( | |
get: { selectedLandmark != nil }, | |
set: { if !$0 { selectedLandmark = nil } } | |
)) { | |
if let selectedLandmark = selectedLandmark { | |
LandmarkDetailView(landmark: selectedLandmark) | |
} | |
} | |
} | |
Text("\(String(describing: currentIndex))").font(.caption).padding(.top, 16) | |
Spacer() | |
} | |
} | |
} | |
struct LandmarkDetailView: View { | |
let landmark: PreviewWrapper.Landmark | |
var body: some View { | |
VStack { | |
Image(landmark.image) | |
.resizable() | |
.aspectRatio(contentMode: .fit) | |
.padding() | |
Text(landmark.name) | |
.font(.largeTitle) | |
.bold() | |
.padding() | |
Spacer() | |
} | |
.navigationTitle(landmark.name) | |
} | |
} | |
return PreviewWrapper() | |
//.environment(\.layoutDirection, .rightToLeft) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment