diff options
| author | Dustin Mierau <dustin@mierau.me> | 2023-12-06 22:52:09 -0800 |
|---|---|---|
| committer | Dustin Mierau <dustin@mierau.me> | 2023-12-06 22:52:09 -0800 |
| commit | e3392bb948ad53889ad334140952f2acf614668e (patch) | |
| tree | 74937a9202b3bb71f28ef9d8e0b3560bdd195b49 /Hotline/Utility/ObservableScrollView.swift | |
| parent | 195af38221d1a4358455ba8501b87e4098da101b (diff) | |
Some work on news. More UI tweaks across the board.
Diffstat (limited to 'Hotline/Utility/ObservableScrollView.swift')
| -rw-r--r-- | Hotline/Utility/ObservableScrollView.swift | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Hotline/Utility/ObservableScrollView.swift b/Hotline/Utility/ObservableScrollView.swift new file mode 100644 index 0000000..a6f46cc --- /dev/null +++ b/Hotline/Utility/ObservableScrollView.swift @@ -0,0 +1,38 @@ +import SwiftUI + +struct ScrollViewOffsetPreferenceKey: PreferenceKey { + typealias Value = CGFloat + static var defaultValue = CGFloat.zero + static func reduce(value: inout Value, nextValue: () -> Value) { + value += nextValue() + } +} + +struct ObservableScrollView<Content>: View where Content : View { + @Namespace var scrollSpace + @Binding var scrollOffset: CGFloat + let content: () -> Content + + init(scrollOffset: Binding<CGFloat>, + @ViewBuilder content: @escaping () -> Content) { + _scrollOffset = scrollOffset + self.content = content + } + + var body: some View { + ScrollView { + content() + .background(GeometryReader { geo in + let offset = -geo.frame(in: .named(scrollSpace)).minY + Color.clear + .preference(key: ScrollViewOffsetPreferenceKey.self, + value: offset) + }) + + } + .coordinateSpace(name: scrollSpace) + .onPreferenceChange(ScrollViewOffsetPreferenceKey.self) { value in + scrollOffset = value + } + } +} |