我目前正在尝试重构一些用于绘制用户输入的SwiftUI代码,以便将更少的内容包含在ContentView中。
这样做,我创建了一个结构,其中包含跟踪用户图形所需的大多数有状态元素。
以下是该枚举的样子:
import Foundation
import SwiftUI
struct DrawableStaff{
var drawing: Drawing = Drawing()
var drawingList: [Drawing] = [Drawing]()
var paths: [Path] = [Path]()
var PathHolder = Path()
var color = Color.primary
var lineWidth: CGFloat = 3.0
}
接下来,我重构了绘图代码以更好地适合枚举:
struct DrawingPad: View {
@Binding var drawableStaff: DrawableStaff
var body: some View {
GeometryReader { geometry in
Path { path in
for drawing in self.drawableStaff.drawings {
self.add(drawing: drawableStaff.drawing, toPath: &path)
}
self.add(drawing: self.drawableStaff.currentDrawing, toPath: &path)
self.drawableStaff.pathHolder = path
}
.stroke(self.drawableStaff.color, lineWidth: self.drawableStaff.lineWidth)
.background(Color(UIColor.systemBackground))
.gesture(
DragGesture(minimumDistance: 0.1)
.onChanged({ (value) in
let currentPoint = value.location
if currentPoint.y >= 0
&& currentPoint.y < geometry.size.height {
self.drawableStaff.currentDrawing.points.append(currentPoint)
}
})
.onEnded({ (value) in
self.drawableStaff.drawings.append(self.currentDrawing)
self.drawableStaff.currentDrawing = Drawing()
})
)
}
.frame(maxHeight: .infinity)
}
我正在尝试通过使用
在ContentView的正文中调用DrawingPadstruct ContentView: View {
@State private var staff1DrawableStaff = DrawableStaff()
var body: some View {
VStack {
let staff1 = ZStack{
DrawingPad(drawableStaff: $staff1DrawableStaff)
}
}
}
在定义DrawingPad期间,有关drawableStaff的内部成员的访问的错误现在很多。如何解决这些错误?
“无法将类型为“绑定”的值转换为预期的参数类型为“绘图””