Thanks for your answer @Tylor. I'm trying to reproduce a situation I've noticed in an open-source library that I created so the code you see here is not Production code per se. It's just some dummy code that reproduces the issue.
I've spent some time working on your example to make it look more similar to the code where I noticed this strange behavior. If you copy-paste this into a project, you'll reproduce (use ContentView as your first view):
struct OtherView: View {
		
		let size: CGSize
		@Binding var page: Int
		@State var draggingOffset: CGFloat = 0
		var body: some View {
				Text(text())
						.frame(width: size.width,
									 height: size.height / 2)
						.background(Color.blue)
						.gesture(
								DragGesture(minimumDistance: 15)
										.onChanged { value in
												withAnimation {
														draggingOffset = value.translation.width
												}
										}
										.onEnded { _ in
												print("\n\n* RESET **")
												withAnimation {
														page += 1
														draggingOffset = 0
												}
										}
						)
		}
		func text() -> String {
				print("page: \(page) offset: \(draggingOffset)")
				return "page: \(page) offset: \(draggingOffset)"
		}
}
struct ContentView: View {
		@State private var value: Int = 0
		var body: some View {
				NavigationView {
						GeometryReader { proxy in
								OtherView(size: proxy.size,
													page: $value)
						}
				}
				.navigationViewStyle(StackNavigationViewStyle())
		}
}
If you use this code you'll see this prints something similar to:
page: 0 offset: 0.0
...
...
page: 0 offset: -239.3333282470703
RESET **
page: 1 offset: -239.3333282470703
page: 1 offset: 0.0
Notice how the state is updated in two steps: first page increments and then draggingOffset resets. The reason why I have a GeometryReader in ContentView is because I need to know the space available to make some calculations. I have a bunch of variables depending on this size so by having a proxy view I can have the size as a dependency so I don't need to be passing as an argument to make these calculations.
If you take that GeometryReader into OtherView and remove the dependency then you'll see how the console prints differently:
RESET **
page: 1 offset: 0.0
If you remove the NavigationView, then the log shows a correct behavior and prints the same as above. So this makes me think there's something wrong in this combination NavigationView, GeometryReader and proxy view.
Post
Replies
Boosts
Views
Activity
Hi, I’m having a similar issue, altho not exactly the same. In my case, GeometryReader is making my view update in two steps (first it updates state variables and then a binding).
Did you find any workaround or any reason why this was happening to you?