Created
December 12, 2022 20:17
-
-
Save IntegerOverlord/e022fd954a118842df6a9aaed3c499de to your computer and use it in GitHub Desktop.
ScrollView approach
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Test: View { | |
private let coordinateSpaceName = "StickyHeaderCoordinateSpaceName" | |
@State var counter: Int = 0 | |
@State var forEachId: UUID = UUID() | |
var body: some View { | |
ScrollView { | |
LazyVStack { | |
GeometryReader { proxy in | |
Color.red | |
.preference( | |
key: ScrollOffsetPreferenceKey.self, | |
value: proxy.frame(in: .named(self.coordinateSpaceName)).origin | |
) | |
} | |
.frame(height: 100) | |
ForEach(0..<10000) { | |
TestCell(viewModel: TestCellViewModel(number: $0)) | |
.onAppear { | |
self.counter += 1 | |
print("Counter: \(self.counter)") | |
if self.counter > 200 { | |
print("Counter overflown \n\n\n") | |
self.forEachId = UUID() | |
self.counter = 0 | |
} | |
} | |
} | |
} | |
.id(self.forEachId) | |
} | |
.coordinateSpace(name: self.coordinateSpaceName) | |
.onPreferenceChange(ScrollOffsetPreferenceKey.self, perform: self.handleOffsetChange) | |
} | |
func handleOffsetChange(to offset: CGPoint) { | |
print("SCROLL OFFSET: \(offset.y)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment