Update Line.swift. Add property color
// import Foundation
import UIKit
import CoreGraphics
struct Line {
var begin = CGPoint.zero
var end = CGPoint.zero
var width: CGFloat = 10
var color: UIColor = .black
}
Update touchesBegan()
// let newline = Line(begin: location, end: location, width: currentLineWidth)
let newline = Line(begin: location, end: location, width: currentLineWidth, color: currentLineColor)
Update touchesEnded()
line.color = currentLineColor
finishedLines.append(line)
Update stroke(_ line: Line)
func stroke(_ line: Line) {
line.color.setStroke()
Update init?(coder aDecoder: NSCoder)
let swipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(DrawView.swipeForColors(_:)))
swipeRecognizer.numberOfTouchesRequired = 3
swipeRecognizer.direction = .up
addGestureRecognizer(swipeRecognizer)
moveRecognizer = UIPanGestureRecognizer(target: self, action: #selector(DrawView.moveLine(_:)))
moveRecognizer.delegate = self
moveRecognizer.cancelsTouchesInView = false
moveRecognizer.require(toFail: swipeRecognizer)
addGestureRecognizer(moveRecognizer)
New methods
func swipeForColors(_ gestureRecognizer: UISwipeGestureRecognizer) {
print("Recognized a swipe")
currentLines.removeAll()
// Grab the menu controller
let menu = UIMenuController.shared
// Make DrawView the target of menu item action messages
becomeFirstResponder()
let color0Item = UIMenuItem(title: "Black", action: #selector(DrawView.selectColor0(_:)))
let color1Item = UIMenuItem(title: "Gray", action: #selector(DrawView.selectColor1(_:)))
let color2Item = UIMenuItem(title: "Red", action: #selector(DrawView.selectColor2(_:)))
let color3Item = UIMenuItem(title: "Yellow", action: #selector(DrawView.selectColor3(_:)))
let color4Item = UIMenuItem(title: "Blue", action: #selector(DrawView.selectColor4(_:)))
menu.menuItems = [color0Item, color1Item, color2Item, color3Item, color4Item]
// Tell the menu where it should come from and show it
let targetRect = CGRect(x: self.frame.midX, y: self.frame.midY, width: 2, height: 2)
menu.setTargetRect(targetRect, in: self)
menu.setMenuVisible(true, animated: true)
}
func selectColor0(_ sender: UIMenuController) {
currentLineColor = .black
UIMenuController.shared.setMenuVisible(false, animated: true)
}
func selectColor1(_ sender: UIMenuController) {
currentLineColor = .gray
UIMenuController.shared.setMenuVisible(false, animated: true)
}
func selectColor2(_ sender: UIMenuController) {
currentLineColor = .red
UIMenuController.shared.setMenuVisible(false, animated: true)
}
func selectColor3(_ sender: UIMenuController) {
currentLineColor = .yellow
UIMenuController.shared.setMenuVisible(false, animated: true)
}
func selectColor4(_ sender: UIMenuController) {
currentLineColor = .blue
UIMenuController.shared.setMenuVisible(false, animated: true)
}