I am on the part where it says build and run and draw some lines. However, I can’t draw any lines. I imagine there is a spelling error but I can’t find it
var currentLine: Line?
var finishedLines = [Line]()
func stroke(_ line: Line) {
let path = UIBezierPath()
path.lineWidth = 10
path.lineCapStyle = .round
path.move(to: line.begin)
path.addLine(to: line.end)
path.stroke()
}
override func draw(_ rect: CGRect) {
// Draw finished lines in black
UIColor.black.setStroke()
for line in finishedLines {
stroke(line)
}
if let line = currentLine {
//If there is a line currently being drawn, do it in red
UIColor.red.setStroke()
stroke(line)
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
//Get location of the touch in view's coordinate system
let location = touch.location(in: self)
currentLine = Line(begin: location, end: location)
setNeedsLayout()
}
override func touchesMoved(_ touches: Set<UITouch>, with: UIEvent?) {
let touch = touches.first!
let location = touch.location(in: self)
currentLine?.end = location
setNeedsLayout()
}
func touchesEnded(_touches: Set<UITouch>, with: UIEvent?) {
if var line = currentLine {
let touch = _touches.first!
let location = touch.location(in: self)
line.end = location
finishedLines.append(line)
}
currentLine = nil
setNeedsLayout()
}
Replace all calls to setNeedsLayout with calls to setNeedsDisplay. I missed this in my previous response because I did not really put your code under the electron microscope then.
Make sure that you use DrawView as the Custom Class for the view controller’s view.