title: Overriding drawRect for drawing app
body: <p>I am building a demo drawing application. I am using <strong>touchesMoved:withEvent</strong> to collect my points and adding them to a <strong>CGMutablePathRef</strong>. To stroke the path, I override DrawRect, add the path to the context and stroke the path:</p>
  
  <pre><code>   override func drawRect(rect: CGRect) {
      self.backgroundColor?.set()
      UIRectFill(rect)
  
      let context : CGContextRef = UIGraphicsGetCurrentContext()
      for line in pathArray {
          CGContextAddPath(context, line.structPath)
          CGContextSetLineWidth(context, line.structLineWidth)
          CGContextSetStrokeColorWithColor(context, line.structLineColor.CGColor)
          CGContextSetAlpha(context, lineOpacity)
  
      }
      CGContextSetLineCap(context, kCGLineCapRound)
      CGContextStrokePath(context)
  
      self.empty = false
  }
  
  override func touchesBegan(touches: Set&lt;UITouch&gt;, withEvent event: UIEvent?) {
      if let touch = touches.first as UITouch! {
          previousPoint = touch.previousLocationInView(self)
          previousPreviousPoint = touch.previousLocationInView(self)
          currentPoint = touch.locationInView(self)
      }
      self.touchesMoved(touches, withEvent: event)
  }
  
  override func touchesMoved(touches: Set&lt;UITouch&gt;, withEvent event: UIEvent?) {
  
  
       if let touch = touches.first as UITouch! {
  
          previousPreviousPoint = previousPoint
          previousPoint = touch.previousLocationInView(self)
          currentPoint = touch.locationInView(self)
  
          let mid1 : CGPoint = getMidPoint(previousPoint, p2: previousPreviousPoint)
          let mid2 : CGPoint = getMidPoint(currentPoint, p2: previousPoint)
  
          let subpath : CGMutablePathRef = CGPathCreateMutable()
          CGPathMoveToPoint(subpath, nil, mid1.x, mid1.y)
          CGPathAddQuadCurveToPoint(subpath, nil, previousPoint.x, previousPoint.y, mid2.x, mid2.y)
  
          let bounds : CGRect = CGPathGetBoundingBox(subpath)
          let drawBox : CGRect = CGRectInset(bounds, -2.0 * lineWidth, -2.0 * lineWidth)
  
          let newLine = line(newPath: subpath)
          pathArray.append(newLine)
          self.setNeedsDisplayInRect(drawBox)
      }
  }
  </code></pre>
  
  <p>The above code works as expected, except I am seeing a unexpected result. The "draw box" which gets the bandbox and sets a CGRectInset changes the lineColor of other paths already drawn:</p>
  
  <p><img src="https://i.imgur.com/pXx6krr.png?1" alt="Drawing Problem"></p>
  
  <p>I understand (sort of) why this is happening, but cannot find a solution to this problem. Any suggestions would be most appreciated!</p>