【问题标题】:SwiftUI: Convert from a CGPoint on screen to a CGPoint in a viewSwiftUI:从屏幕上的 CGPoint 转换为视图中的 CGPoint
【发布时间】:2021-04-15 16:14:11
【问题描述】:

我的 ContentView 中某处放置了一个视图。我希望能够将图像拖放到它上面。所以我添加了 .onDrop 。在 UIKit 中,我会使用 UniverseView.convert(point, to: targetView)。如何在 SwiftUI 中执行此操作?

示例实用程序可以是在橙色方块上绘制已放置图像的缩略图

struct ContentView: View {
    @State var isTargeted: Bool = false
    
    var body: some View {
        VStack {
            
            Spacer()
                .frame(width: 600, height: 200)
                .background(Color.yellow.opacity(0.3))

            HStack {
                Spacer()
                    .frame(width: 200, height: 200)
                    .background(Color.yellow.opacity(0.3))

                Spacer()
                    .frame(width: 200, height: 200)
                    .background(Color.orange)
                    .onDrop(of: [ "public.image"], isTargeted: $isTargeted, perform: { (providers, point) -> Bool in
                        
                        print(point) // i.e. (x:336.0, y:486.5)
                                     // but I want it in the context of this views frame
                        // let bodyView: View
                        // let targetView: View
                        // let thePoint = bodyView.convert(point, to: targetView)
                        return true
                        
                    })

                Spacer()
                    .frame(width: 200, height: 200)
                    .background(Color.yellow.opacity(0.3))

            }
            .frame(width: 600, height: 200)

            Spacer()
                .frame(width: 600, height: 200)
                .background(Color.yellow.opacity(0.3))
        }
        .frame(width: 600, height: 600)
    }
}

【问题讨论】:

    标签: swift swiftui cgpoint


    【解决方案1】:
    extension GeometryProxy {
        // converts from some other coordinate space to the proxy's own
        func convert(_ point: CGPoint, from coordinateSpace: CoordinateSpace) -> CGPoint {
            let frame = self.frame(in: coordinateSpace)
            let localViewPoint = CGPoint(x: point.x-frame.origin.x, y: point.y-frame.origin.y)
            
            // Some system bug? Test in iOS 13.4, 13.5, 14.1, 14.5
            if coordinateSpace == .global {
                switch (UIDevice.current.systemVersion as NSString).floatValue {
                case 13.5:
                    return CGPoint(x: localViewPoint.x, y: point.y)
                case 14.5:
                    return point
                default: break
                }
            }
            
            return localViewPoint
        }
    }
    
    

    像这样使用:geometry.convert(location, from: .global)

    【讨论】:

    • 感谢您的回答!考虑在代码中添加一些上下文或简短解释它的作用/解决问题的原因。
    • 从CS193p-cs193p.sites.stanford.edu复制,但是14.5还是有问题,不知道为什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 2013-04-04
    • 2017-07-29
    • 1970-01-01
    相关资源
    最近更新 更多