😓 일단 너무나도 힘들었다.. 오늘 하루종일 이것만 찾아보고 → 구현하고 → 다시 찾아보고의 연속이었다.
ZStack(alignment: .topTrailing) {
Image(image[0])
.resizable()
.aspectRatio(0.8, contentMode: .fill)
if image.count > 1 {
Image(systemName: "rectangle.fill.on.rectangle.fill")
.foregroundStyle(.white)
.padding(5)
}
}
containerRelativeFrame(axis, count:, span:, spacing)scrollTargetBehavior(.paging)ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 0) {
ForEach(card.image, id:\\.self) { image in
ZStack {
Image(image)
.resizable()
.overlay(alignment: .topTrailing, content: {
NavigationLink {
ZoomCardView(image: image)
} label: {
Image(systemName: "square.arrowtriangle.4.outward")
.padding()
.foregroundStyle(.white)
}
})
.scaledToFit()
.containerRelativeFrame(.horizontal, count: card.image.count, span: card.image.count, spacing: 0)
.onTapGesture(count: 2, perform: {
...
})
if showHeart {
...
}
}
}
}
}
.scrollIndicators(.hidden)
.scrollTargetBehavior(.paging)

scroll을 할때, 각 페이지의 높이에 따라 HStack의 높이를 조절하는 것..
(결국 구현 못했다)
geometryReader와.coordinateSpace, preferenceKey를 사용해서 scrollView에서 사용자의 스크롤 offset을 실시간으로 구할 수 있는 걸 알아냈다..!
import SwiftUI
struct DetectScrollPosition: View {
@State private var scrollPosition: CGPoint = .zero
@State private var scrollIndex: Int = 0
var box: [(Color, CGFloat)] = [(.red, 100), (.blue, 200), (.yellow, 300)]
var body: some View {
NavigationView {
ScrollView(.horizontal) {
HStack {
ForEach(box.indices) { index in
Rectangle()
.fill(box[index].0)
.frame(width: 400, height: box[index].1)
.id(index)
}
}
.frame(height: box[scrollIndex].1)
.border(Color.black)
.background(GeometryReader { geometry in
Color.clear
.preference(key: ScrollOffsetPreferenceKey.self, value: geometry.frame(in: .named("scroll")).origin)
})
.onPreferenceChange(ScrollOffsetPreferenceKey.self) { value in
self.scrollPosition = value
scrollIndex = Int((abs(value.x) + 200) / 400)
print(scrollIndex)
}
}
.coordinateSpace(name: "scroll")
.navigationTitle("Scroll offset: \(scrollPosition.x)")
.navigationBarTitleDisplayMode(.inline)
}
}
}
struct DetectScrollPosition_Previews: PreviewProvider {
static var previews: some View {
DetectScrollPosition()
}
}
struct ScrollOffsetPreferenceKey: PreferenceKey {
static var defaultValue: CGPoint = .zero
static func reduce(value: inout CGPoint, nextValue: () -> CGPoint) {
}
}
https://saeedrz.medium.com/detect-scroll-position-in-swiftui-3d6e0d81fc6b