title: How to clip in drawRect when drawing (and clipping) based upon CGMutablePathRefs
body: <p>In my custom control I have defined a few CGMutablePathRefs with the needed lines and arcs to draw my control; one draws the overall fill shape and others provide specular highlights.  I have also defined two CGMutablePathRefs which contain the paths needed as clipping masks for the active and inactive state of the control.</p>
  
  <p>What I'm struggling with is applying the clipping paths.  I have previously used clipping paths for applying gradients to an image, but those drawing commands were of the CGContext... variety, not the CGPath... variety.</p>
  
  <p>For testing purposes I have removed the specular highlight drawing aspects, just trying to get a large path clipped to a smaller path.  This is what I had been testing with:</p>
  
  <pre><code>- (void)drawRect:(CGRect)rect
  {
      CGContextRef ctx = UIGraphicsGetCurrentContext();
      CGContextBeginPath(ctx);
      CGContextAddPath(ctx, inactiveClip); 
      CGContextClosePath(ctx);
      CGContextClip(ctx);
  
      CGContextAddPath(ctx, frontFace);
      CGContextSetLineWidth(ctx, 1.0);
      CGContextSetFillColorWithColor(ctx, [[UIColor blackColor] CGColor]);
      CGContextFillPath(ctx);
  
  }
  </code></pre>
  
  <p>By putting the clipping command before any drawing, I thought I was saying to CoreGraphics, "Here's the region you should actually draw into."</p>
  
  <p>Alas, nothing is drawn.</p>
  
  <p>So assuming I had that ordering backwards I tried:</p>
  
  <pre><code>- (void)drawRect:(CGRect)rect
  {
      CGContextRef ctx = UIGraphicsGetCurrentContext();
  
      CGContextAddPath(ctx, frontFace);
      CGContextSetLineWidth(ctx, 1.0);
      CGContextSetFillColorWithColor(ctx, [[UIColor blackColor] CGColor]);
      CGContextFillPath(ctx);
  
      CGContextBeginPath(ctx);
      CGContextAddPath(ctx, inactiveClip); 
      CGContextClosePath(ctx);
      CGContextClip(ctx);
  
  }
  </code></pre>
  
  <p>This was to say to CoreGraphics, "Okay before you actually color bits, check them against this clipping region."</p>
  
  <p>Alas, nothing is clipped.</p>
  
  <p>Since it is the case that my clipping path uses some of the same points and control points, in the same order, as the fill path, I have also replaced the call to CGContextClip with a call to CGContextEOClip to see if I was really struggling with the even-odd rule, but that doesn't seem to have had any visual affect.</p>
  
  <p>I don't really know if I needed to bracket the CGContextAddPath call with CGContextBeginPath/CGContextClosePath calls, but what I was trying to do was minimize the differences between Apple's example code and my code.  In theirs they do their CGContext... drawing calls between begin/close calls so I was too.</p>
  
  <p>What am I misunderstanding here?</p>