Silver Solution with Swift


A quick preface. I was on Chapter 6 of this book when Apple launched the Swift language at WWDC. I felt a mixture of excitement and frustration. “Just when I felt like I was getting the hang of Obj-C”, I thought. After going over the official iBooks guide and the pre-release documentation I decided to restart the book and complete each chapter in both Obj-C and Swift.
My solutions are bound to be rough around the edges and full of rookie mistakes but it gives each chapter, and the whole book, an extra level of complexity.

Think of each chapter having a new “Platinum Challenge”.

Anyway, here is my solution for the Silver Challenge. It does leave one bug - Once a line is selected and the UIMenuController is visible, you cannot long-tap the same line and drag around, you must deselect and then long tap to achieve the desired result. Any suggestions would be greatly appreciated.

[code]func moveLine(gesture:UIPanGestureRecognizer) {

    //The property selectedLine is an optional to allow for a nil-check
    if !selectedLine {
    let menu = UIMenuController.sharedMenuController()
    //If the UIMenuController is visible, a line must be selected. We must deselect the line to avoid draging it along with our newly drawn line
    if menu.menuVisible == true {
        selectedLine = nil
        menu.setMenuVisible(false, animated: true)
    if gesture.state == UIGestureRecognizerState.Changed {
        var translation = gesture.translationInView(self)
        var begin = selectedLine!.begin
        var end = selectedLine!.end
        begin.x += translation.x
        begin.y += translation.y
        end.x += translation.x
        end.y += translation.y
        selectedLine!.begin = begin
        selectedLine!.end = end
        gesture.setTranslation(CGPointZero, inView: self)