简体   繁体   中英

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.

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
                })
                )
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM