如何获取鼠标指针相对于应用程序窗口的xy位置?

问题描述 投票:0回答:0

我想知道鼠标指针相对于应用程序窗口的当前位置(xy),并将其提供给状态变量

positionME
。为什么会抛出错误?

Value of type 'GeometryProxy' has no member 'location'
Cannot convert value of type '()' to expected argument type '(() -> Void)?'

struct ContentView: View {
    
    @State private var positionME: CGPoint?
    
    var body: some View {
        VStack {
            
            GeometryReader { gp in
                
                Rectangle()
                    .fill(.green)
                    .frame(width: 100, height: 100)
                    .onAppear(perform: positionME = $0.location)
            }
            .padding()
        }}
}
swiftui location position
© www.soinside.com 2019 - 2024. All rights reserved.