Turning Touches into Lines

A line is defined by two points. Your Line stores these points as properties named begin and end. When a touch begins, you will create a Line and set both of its properties to the point where the touch began. When the touch moves, you will update the Line’s end. When the touch ends, you will have your complete Line.

In DrawView.swift, implement touchesBegan(_:withEvent:) to create a new line.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let touch = touches.first!

    // Get location of the touch in view's coordinate system
    let location = touch.locationInView(self)

    currentLine = Line(begin: location, end: location)

    setNeedsDisplay()
}

This code first figures out the location ...

Get iOS Programming: The Big Nerd Ranch Guide 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.