Open1

SwiftUIでページングなスクロール(iOS17~)

だーじーだーじー

iOS17~のscrollTargetBehavior(_:)で簡単に実現できるようになったようです

import SwiftUI

struct PagingView: View {
    var body: some View {
        GeometryReader { proxy in
            ScrollView(.horizontal) {
                LazyHStack(spacing: 16) {
                    ForEach(0..<10, id: \.self) { _ in
                        Rectangle()
                            .frame(width: proxy.size.width - 64, height: 200)
                    }
                }
            }
            .scrollTargetBehavior(.paging)
            .frame(height: proxy.size.height)
            .safeAreaPadding(.horizontal, 32)
        }
    }
}

#Preview {
    PagingView()
}