I need some direction to resolve a problem. I have multiple videos inside a scroll view and I want only one of them to play at a time. I know about geo.frame to know the position of the view in screen but how can I constantly check if the screen moved to a point? I want to use this code in the video player view but I can only put this to .onAppear and it won't work because it is only called once. Is there a method that I can check when the screen is moved (scroll view is dragged) so that I can play the video in the middle and stop the other ones?
if (geo.frame(in: .global).midY > 200 && geo.frame(in: .global).midY < 800) {
avPlayer.play()
print("Global center: \(geo.frame(in: .global).midX) x \(geo.frame(in: .global).midY)")
print("Video to play: \(postID)")
}
You can put this code inside your view body using the Geometry reader. The value of geo is updated as the items are being scrolled and moved around. Put this in a view of a row in your table of videos:
struct Example: View {
private func checkLocation(geo: GeometryProxy) -> Bool {
let loc = geo.frame(in: .global).midY
print ("Loc is: \(loc)")
if loc > 200 {
return true
}
return false
}
var body: some View {
GeometryReader { geo in
if checkLocation(geo: geo) {
Text ("This is working")
}
}
}
}