dipon_the_pro_coder
dipon_the_pro_coder

Reputation: 45

How to get location of a view- swiftUI

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

Answers (1)

ChrisR
ChrisR

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

Related Questions