I have used core graphics in a view to drawing lines. Now, I need to erase the lines that I daw using an erase button. I don't know what should I do to get a clear view with no lines.
Here is the code I used for drawing lines.
struct Line {
var points: [CGPoint]
var strokeColor: UIColor
var strokeWidth: CGFloat
}
class CanvasView: UIView {
private var lines: [Line] = []
private var strokeWidth: CGFloat = 8.0
private var strokeColor: UIColor = .white
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else { return }
lines.forEach { line in
context.setStrokeColor(line.strokeColor.cgColor)
context.setLineWidth(line.strokeWidth)
context.setLineCap(.round)
for (index, point) in line.points.enumerated() {
if index == 0 {
context.move(to: point)
} else {
context.addLine(to: point)
}
}
context.strokePath()
}
}
override func touchesBegan(_ touches: Set<UITouch>, with: event: UIEvent?) {
guard let point = touches.first.location(in: self) else { return }
let newLine = Line(points: [point], strokeColor: strokeColor, strokeWidth: strokeWidth)
lines.append(newLine)
setNeedsDisplay()
}
override func touchesMoved(_ touches: Set<UITouch>, with: event: UIEvent?) {
guard let point = touches.first.location(in: self) else { return }
guard var lastLine = lines.popLast() else { return }
lastLine.points.append(point)
lines.append(lastLine)
setNeedsDisplay()
}
}
I have connected this CanvasView: UIView class to my own view to use Core Graphics Context drawing.
I need a way to erase what I draw in my UIView using a button click.
Your drawing is depends on your list of Line
so the thing you just need to do is make func where you clear all your lines then redraw. So at this time, when you call draw(_ rect:)
again check if there is no value in your lines
then clear view.
class CanvasView: UIView {
private var lines: [Line] = []
private var strokeWidth: CGFloat = 8.0
private var strokeColor: UIColor = .white
func clearView() {
self.lines = []
setNeedsDisplay()
}
override func draw(_ rect: CGRect) {
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else { return }
if self.lines.count == 0 {
// context clear here
context.clear(self.bounds)
}
lines.forEach { line in
context.setStrokeColor(line.strokeColor.cgColor)
context.setLineWidth(line.strokeWidth)
context.setLineCap(.round)
for (index, point) in line.points.enumerated() {
if index == 0 {
context.move(to: point)
} else {
context.addLine(to: point)
}
}
context.strokePath()
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: self) else { return }
let newLine = Line(points: [point], strokeColor: strokeColor, strokeWidth: strokeWidth)
lines.append(newLine)
setNeedsDisplay()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: self) else { return }
guard var lastLine = lines.popLast() else { return }
lastLine.points.append(point)
lines.append(lastLine)
setNeedsDisplay()
}
}
And in your ViewController
just simply call func canvasView.clearView()