Skip to content

Instantly share code, notes, and snippets.

@Polenoso
Created November 27, 2023 22:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Polenoso/776524e9505ba79878cd45f6340d798b to your computer and use it in GitHub Desktop.
Save Polenoso/776524e9505ba79878cd45f6340d798b to your computer and use it in GitHub Desktop.
A View Modifier for a Paged Index View like, using ScrollToTargetBehavior
//
// PagedIndexViewModifier.swift
//
// Copyright© 2023 ApiumHub
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import SwiftUI
@available(iOS 17.0, *)
struct PagedIndexViewModifier: ViewModifier {
@Binding var index: Int
@State private var offset: CGPoint = .zero
func body(content: Content) -> some View {
GeometryReader { contentProxy in
ScrollViewReader { svReaderProxy in
ScrollView {
LazyVStack(spacing: 0) {
content
.frame(width: contentProxy.size.width, height: contentProxy.size.height)
}
.background(GeometryReader { geometry in
Color.clear
.preference(key: ScrollOffsetPreferenceKey.self, value: geometry.frame(in: .named("scroll")).origin)
})
.onPreferenceChange(ScrollOffsetPreferenceKey.self) { value in
self.offset = value
changeIndexIfNeeded(with: value, contentSize: contentProxy.size)
}
}
.scrollTargetBehavior(.paging)
.scrollIndicators(.hidden)
.onChange(of: index) { oldValue, newValue in
guard oldValue != newValue else {
return
}
svReaderProxy.scrollTo(newValue)
}
}
}
}
private func changeIndexIfNeeded(with scrollValue: CGPoint, contentSize: CGSize) {
let yPoint = Double(scrollValue.y)
if yPoint == 0 && index == 0 {
return
}
let height = contentSize.height
guard yPoint.truncatingRemainder(dividingBy: height) == 0 else { return }
let currentIndex = index
let expectedIndex = Int(abs(yPoint / (height)))
if expectedIndex == currentIndex { return }
index = expectedIndex
}
}
@available(iOS 17.0, *)
extension View {
func indexedPageView(_ index: Binding<Int>) -> some View {
modifier(PagedIndexViewModifier(index: index))
}
}
fileprivate struct ScrollOffsetPreferenceKey: PreferenceKey {
static var defaultValue: CGPoint = .zero
static func reduce(value: inout CGPoint, nextValue: () -> CGPoint) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment