在下面的代码中,我正在读取预定义路径并将轴系统从 SwiftUI 转换为数学轴。到目前为止一切顺利。当我向此路径添加拖动手势时,问题出现了。该手势仅适用于形状的特定部分(如照片所示),而不适用于路径的其余部分。如何在不改变预定义路径的情况下解决此问题?
import SwiftUI
struct ContentView: View {
@State private var pathCurrentOffset: CGSize = CGSize()
@State private var pathLastOffset: CGSize = CGSize()
var body: some View {
GeometryReader { geometryValue in
Color.white
myCustomPathTest
.scale(x: 1.0, y: -1.0)
.stroke(Color.black, lineWidth: 5.0)
.offset(x: pathCurrentOffset.width + geometryValue.size.width/2.0,
y: -pathCurrentOffset.height - geometryValue.size.height/2.0)
.gesture(pathDragGesture)
}
.padding()
}
private var pathDragGesture: some Gesture {
return DragGesture(minimumDistance: 0.0, coordinateSpace: .local)
.onChanged() { gestureValue in
pathCurrentOffset = CGSize(width: gestureValue.translation.width + pathLastOffset.width,
height: -gestureValue.translation.height + pathLastOffset.height)
}
.onEnded() { gestureValue in
pathLastOffset = pathCurrentOffset
}
}
}
let myCustomPathTest: Path = {
return Path { path in
path.move(to: .zero)
path.addLine(to: CGPoint(x: 50.0, y: 50.0))
path.addEllipse(in: CGRect(origin: CGPoint(x: -50.0, y: -50.0), size: CGSize(width: 100.0, height: 100.0)))
}
}()
路径上的工作部分是: