swift - SwiftUI: Convert from a CGPoint on screen to a CGPoint in a view
问题描述
I have a view placed somewhere in my ContentView. I want to be able to drop images onto it. So I've added and .onDrop to it. In UIKit, I would use universeView.convert(point, to: targetView). How do I do this in SwiftUI?
Example utility could be to draw the thumbnail of the dropped image where it was dropped on the orange square
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)
}
}
解决方案
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)
推荐阅读
- java - 如何在资产文件夹中显示来自 sqlite 数据库的数据?
- git - 当我推送到远程时出现权限被拒绝错误(请确保您具有正确的访问权限并且存储库存在)
- javascript - 动态查询谷歌表格中的范围
- node.js - 将 MongoDb 从 cloud9 复制到 Mlab,新文档以奇数顺序保存到 DB
- node.js - 通过函数检查 Json 字段是否为 Null 或不存在
- javascript - 使提交按钮也发送一个额外的值
- reporting-services - TFS 构建服务器因 Reporting Services 项目而失败
- mysql - 无法从 Intellij 连接到在 docker 容器中运行的 mySql - “指定的数据库用户/密码组合被拒绝”
- php - 搜索到的词没有在我的数据库中正确注册
- vb.net - 如何在 VB.Net 2003 中创建按钮以打开另一个 apx.net 网页