mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-05-28 21:28:22 +00:00
45 lines
1.1 KiB
Swift
45 lines
1.1 KiB
Swift
|
/*! @copyright 2021 Medium */
|
||
|
|
||
|
import SwiftUI
|
||
|
|
||
|
// Source: https://www.fivestars.blog/articles/scrollview-offset/
|
||
|
|
||
|
public struct ScrollViewOffsetReader<Content: View>: View {
|
||
|
let onOffsetChange: (CGFloat) -> Void
|
||
|
let content: () -> Content
|
||
|
|
||
|
public init(
|
||
|
onOffsetChange: @escaping (CGFloat) -> Void,
|
||
|
@ViewBuilder content: @escaping () -> Content
|
||
|
) {
|
||
|
self.onOffsetChange = onOffsetChange
|
||
|
self.content = content
|
||
|
}
|
||
|
|
||
|
public var body: some View {
|
||
|
ScrollView {
|
||
|
offsetReader
|
||
|
content()
|
||
|
.padding(.top, -8)
|
||
|
}
|
||
|
.coordinateSpace(name: "frameLayer")
|
||
|
.onPreferenceChange(OffsetPreferenceKey.self, perform: onOffsetChange)
|
||
|
}
|
||
|
|
||
|
var offsetReader: some View {
|
||
|
GeometryReader { proxy in
|
||
|
Color.clear
|
||
|
.preference(
|
||
|
key: OffsetPreferenceKey.self,
|
||
|
value: proxy.frame(in: .named("frameLayer")).minY
|
||
|
)
|
||
|
}
|
||
|
.frame(height: 0)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private struct OffsetPreferenceKey: PreferenceKey {
|
||
|
static var defaultValue: CGFloat = .zero
|
||
|
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {}
|
||
|
}
|