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 {
        stroke(line)
    }

    if let line = currentLine {
        // If there is a line currently being drawn, do it in red
        UIColor.red.setStroke()
        stroke(line)
    }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset