【发布时间】: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)
}
}
【问题讨论】: