Hi @WBJ
I don't know if I'm too late with a proposition. ScrollViewReader
seems only be usable to programatically move the scrolling position to an element. If you want to read the position of an element, you will have to use GeometryReader
.
I've implemented a simple detection whether we reached the end of a scroll view by adding a hidden Text
element at the end.
Comparing the Y position of this element with the heigth of the ScrollView
, I can trigger an action as soon as it becomes visible.
This is something which might also be usable to load new elements as soon as one reaches the end of a list.
struct ContentView: View {
@State private var readToEnd = false
@State private var scrollViewHeight = CGFloat.infinity
@Namespace private var scrollViewNameSpace
var body: some View {
NavigationView {
VStack {
ScrollView {
Text(eula)
Text("hidden tag")
.hidden()
.background(
GeometryReader { proxy in
Color.clear
.onChange(of: proxy.frame(in: .named(scrollViewNameSpace))) { newFrame in
if newFrame.minY < scrollViewHeight {
readToEnd = true
}
}
}
)
.border(Color.red)
}
.background(
GeometryReader { proxy in
Color.clear
.onChange(of: proxy.size, perform: { newSize in
let _ = print("ScrollView: ", newSize)
scrollViewHeight = newSize.height
})
}
)
.coordinateSpace(name: scrollViewNameSpace)
NavigationLink("Accept", destination: Text("Welcome and Thank you!"))
.opacity(readToEnd ? 1 : 0)
.animation(.default, value: readToEnd)
}
.padding(.horizontal)
.navigationTitle("EULA")
}
}
let eula = Array(repeating: """
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
""", count: 10).joined(separator: "").trimmingCharacters(in: .whitespacesAndNewlines)
}
See the video I've added to the Gist: