Hello,
I was following a tutorial on how to use the SwiftUI Canvas and I can't seem to get it to work. Below is the code that I have.
struct Line {
var points: [CGPoint]
var color: Color
}
struct CanvasView: View {
@State var lines: [Line] = []
@State var selectedColor: Color = Color.black
var body: some View {
ScrollView([.horizontal, .vertical]) {
Canvas { ctx, size in
for line in lines {
var path = Path()
path.addLines(line.points)
ctx.stroke(path, with: .color(line.color), style: StrokeStyle(lineWidth: 5, lineCap: .round, lineJoin: .round))
}
}.gesture(DragGesture(minimumDistance: 0, coordinateSpace: .local)
.onChanged({ value in
let position = value.location
if value.translation == .zero {
lines.append(Line(points: [position], color: selectedColor))
} else {
guard let lastIndex = lines.indices.last else {
return
}
lines[lastIndex].points.append(position)
}
})
)
}
}
}
I'm pretty sure I followed the tutorial exactly so I'm not sure why it isn't drawing. Any help would be greatly appreciated. Thank you.