Solution for Ch 19 Gold Challenge: Speed and Size

Update Line.swift

struct Line {
  var lineWidth: CGFloat = 10

Add new properties

class DrawView: UIView, UIGestureRecognizerDelegate {
  var maxRecordedVelocity: CGFloat = CGFloat.leastNonzeroMagnitude
  var minRecordedVelocity: CGFloat = CGFloat.greatestFiniteMagnitude
  var currentVelocity: CGFloat = 0
  var currentLineWidth: CGFloat {
    let maxLineWidth: CGFloat = 20
    let minLineWidth: CGFloat = 1
    // Thinner line for faster velocity
    let lineWidth = (maxRecordedVelocity - currentVelocity) / (maxRecordedVelocity - minRecordedVelocity) * (maxLineWidth - minLineWidth) + minLineWidth
    return lineWidth
  }

Update stroke(_ line: Line)

  let path = UIBezierPath()
//    path.lineWidth = lineThickness
  path.lineWidth = line.width

Update touchesBegan()

// let newline = Line(begin: location, end: location)
let newline = Line(begin: location, end: location, width: currentLineWidth)

Update touchesEnded()

line.width = currentLineWidth
finishedLines.append(line)

Update moveLine(_ gestureRecognizer: UIPanGestureRecognizer)

  print("Recognized a pan")
  
  let velocityInXY = gestureRecognizer.velocity(in: self)
  currentVelocity = hypot(velocityInXY.x, velocityInXY.y)
  
  maxRecordedVelocity = max(maxRecordedVelocity, currentVelocity)
  minRecordedVelocity = min(minRecordedVelocity, currentVelocity)
  
  print("Current Drawing Velocity: \(currentVelocity) points per second")
  print("maxRecordedVelocity: \(maxRecordedVelocity) points per second")
  print("minRecordedVelocity: \(minRecordedVelocity) points per second")
  
  guard longPressRecognizer.state == .changed || longPressRecognizer.state == .ended else {
    return
  }