title: Removing old sections of curved line when drawing new sections in Swift
body: <p>I have Swift 2 code below that draws curved lines when the user touches and moves their finger across the screen. New sections of the curved line are added as the user moves their finger to give one continuous curved line on screen. (top image)</p>
  
  <p>However, I wish to change the below code so that as each new section of the line is added and drawn to screen, the previous section and earlier sections are deleted, so that all is seen on the screen is the new section and nothing else. (bottom image)</p>
  
  <p>What needs to be modified in the section of code below to achieve this?</p>
  
  <p><a href="https://i.stack.imgur.com/CbrXz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CbrXz.png" alt="enter image description here"></a></p>
  
  <pre><code>// Swift 2 code below tested using Xcode 7.0.1.
  
  class drawLines: UIView {
  
  let path=UIBezierPath()
  
  var previousPoint:CGPoint = CGPoint.zero
  
  var strokeColor:UIColor?
  
  required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
  }
  
  override func drawRect(rect: CGRect) {
  
      strokeColor = UIColor.blackColor()
      strokeColor?.setStroke()
      path.lineWidth = 10
      path.stroke()
  
  }
  
  override func touchesBegan(touches: Set&lt;UITouch&gt;, withEvent event: UIEvent?) {
  
      let touch: AnyObject? = touches.first
      let currentPoint = touch!.locationInView(self)
  
      path.moveToPoint(currentPoint)
      previousPoint=currentPoint
      self.setNeedsDisplay()
  }
  
  override func touchesMoved(touches: Set&lt;UITouch&gt;, withEvent event: UIEvent?) {
      let touch: AnyObject? = touches.first
      let currentPoint = touch!.locationInView(self)
      let midPoint = self.midPoint(previousPoint, p1: currentPoint)
  
      path.addQuadCurveToPoint(midPoint,controlPoint: previousPoint)
      previousPoint=currentPoint
  
      self.setNeedsDisplay()
  
      path.moveToPoint(midPoint)
  }
  
  override func touchesEnded(touches: Set&lt;UITouch&gt;, withEvent event: UIEvent?) {
      self.setNeedsDisplay()
  }
  
  override func touchesCancelled(touches: Set&lt;UITouch&gt;?, withEvent event: UIEvent?) {
      self.touchesEnded(touches!, withEvent: event)
  }
  
  func midPoint(p0:CGPoint,p1:CGPoint)-&gt;CGPoint
  {
      let x=(p0.x+p1.x)/2
      let y=(p0.y+p1.y)/2
      return CGPoint(x: x, y: y)
  }
  
  }
  </code></pre>