Drawing with DrawView

An instance of DrawView needs to be able draw lines. You are going to write a method that uses UIBezierPath to create and stroke a path based on the properties of a given Line. Then you will override draw(_:) to draw the lines in the array of finished lines as well as the current line, if any.

In DrawView.swift, implement the method for stroking lines and override draw(_:).

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 ...

Get iOS Programming: The Big Nerd Ranch Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.