【发布时间】:2019-11-22 17:12:09
【问题描述】:
所以我有以下代码:
import SwiftUI
struct ContentView : View {
@State private var draggingLocation = CGPoint.zero
@State private var startLocation = CGPoint.zero
@State private var dragging = false
var body: some View {
let GR = DragGesture(minimumDistance: 10, coordinateSpace: .global)
.onEnded { value in
self.dragging = false
self.draggingLocation = CGPoint.zero
self.startLocation = CGPoint.zero
}
.onChanged { value in
if !self.dragging {
self.dragging = true
}
if self.startLocation == CGPoint.zero {
self.startLocation = value.startLocation
}
self.draggingLocation = value.location
}
return ZStack {
if self.dragging {
Path { path in
path.move(to: CGPoint(x: self.startLocation.x-5, y: self.startLocation.y-5))
path.addLine(to: CGPoint(x: self.draggingLocation.x-5, y: self.draggingLocation.y+5))
path.addLine(to: CGPoint(x: self.draggingLocation.x+5, y: self.draggingLocation.y-5))
path.addLine(to: CGPoint(x: self.startLocation.x+5, y: self.startLocation.y+5))
}
.fill(Color.black)
}
Circle()
.fill(self.dragging ? Color.blue : Color.red)
.frame(width: 100, height: 100)
.gesture(GR)
.offset(
x: 75,
y: 75
)
Circle()
.fill(self.dragging ? Color.blue : Color.red)
.frame(width: 100, height: 100)
.gesture(GR)
.offset(
x: -75,
y: -75
)
}
.frame(width: 400, height: 400)
.background(Color.gray)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
这会导致这种行为:
我希望能够将边缘从一个圆圈拖到另一个圆圈中,问题当然是Path 的坐标空间是相对于灰色框(ContentView)而不是全球的。 Path 在文档中有一个 coordinateSpace 属性,但是关于如何使用它的信息很少,并且使用 SwiftUI 搜索该术语实际上会返回三个结果,所有这些实际上只是指向 Apple 当前稀疏文档的链接。任何人都知道如何最好地解决这个问题?
【问题讨论】:
标签: ios swift ipad ios-simulator swiftui