Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- struct ContentView: View {
- @State private var scrollOffset: CGFloat = 0
- var body: some View {
- ScrollView {
- GeometryReader { geometry in
- Color.clear
- .preference(key: ScrollOffsetPreferenceKey.self, value: geometry.frame(in: .global).minY)
- }
- .frame(height: 0)
- VStack {
- ForEach(0..<100) { index in
- Text("Item \(index)")
- .padding()
- }
- }
- }
- .onPreferenceChange(ScrollOffsetPreferenceKey.self) { value in
- scrollOffset = value
- print("Scroll offset: \(scrollOffset)")
- }
- }
- }
- struct ScrollOffsetPreferenceKey: PreferenceKey {
- typealias Value = CGFloat
- static var defaultValue: CGFloat = 0
- static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
- value = nextValue()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement