Reputation: 45
I want to track location of a view on drag gesture which belongs to a ZStack. So that I can zoom in or out my view . So what is the best way to get location of a view.
Upvotes: 0
Views: 2025
Reputation: 12165
Wrap your view with a GeometryReader
:
struct ContentView: View {
var body: some View {
GeometryReader { geo in
// frame of YourView in global coordinates
// gives you minX, minY, widht, height and more
_ = geo.frame(in: .global)
YourView()
.gesture(DragGesture()
.onChanged({ value in
_ = value.translation.width // relative drag x
_ = value.translation.height // relative drag y
})
)
}
}
}
Upvotes: 1