title: To clear drawing in drawRect shape-layer drawing
body: <p>To save time for anyone googling here, I incorrectly thought that <code>clearsContextBeforeDrawing</code> would <strong>also clear any layers added in drawRect</strong>.  It does not.</p>
  
  <p>Of course as Matt has explained, you should certainly only draw the ordinary way in drawRect, and then <code>clearsContextBeforeDrawing</code> works correctly. Example,</p>
  
  <pre><code>override func drawRect(rect: CGRect)
      {
      // clearsContextBeforeDrawing works perfectly here as normal
      // don't add any layers in here :)
  
      let dotPath = UIBezierPath(ovalInRect:rect)
      mainColor.setFill()
      dotPath.fill()
      let ins:CGFloat = ( ringThickness / 2 )
      let circlePath = UIBezierPath(ovalInRect: CGRectInset(rect,ins,ins) )
      circlePath.lineWidth = ringThickness
      ringColor.setStroke()
      circlePath.stroke()
      }
  </code></pre>
  
  <hr>
  
  <p>Here's a poor way to draw in <code>drawRect</code></p>
  
  <pre><code>override func drawRect(rect: CGRect)
      {
      let circlePath = UIBezierPath(ovalInRect:rect)
      let circle = CAShapeLayer()
      circle.path = circlePath.CGPath
      circle.fillColor = someColor.CGColor
      layer.addSublayer(circle)
  
      ... and a few more layers like ...
      blah.fillColor = someColor.CGColor
      layer.addSublayer(blah)
      }
  </code></pre>
  
  <p>But how to clear before drawing?</p>
  
  <p>Setting <code>clearsContextBeforeDrawing</code> does nothing.</p>
  
  <p>The only way I can clear is by removing all the layers:</p>
  
  <pre><code>override func drawRect(rect: CGRect)
      {
      // first remove all layers...
      if let sublayers = layer.sublayers {
          for layer in sublayers {
              if layer.isKindOfClass(CAShapeLayer) //(essential)
                  { layer.removeFromSuperlayer() }
          }
      }
      ...
      }
  </code></pre>
  
  <p>Is there a better way to clear the "layer" drawing?</p>