CGPathMoveToPoint(thePath, NULL, lastDrawnPt.x, self.bounds.size.height);
It simply displays a series of line segments. For certain point values, this might look something like this:
| |||| |
It does not display a single continuous form. Thus, this path is useless for clipping because it is virtually empty; as you saw, clipping to it will cause the further drawing not to be inside the clipping path.
Each lineto , curveto , arc , etc. works from the current point. First you set moveto , but each lineto , curveto , arc , etc. It does not clear the current point, but updates it. Thus, to create a single shape, you make one moveto , followed by a sequence of lineto (or curveto or arc ), and then a closepath .
Speaking of closepath ...
CGPathCloseSubpath(thePath);
This creates a single closed shape on the way, but since this shape has a zero region (which is only a segment of a line that doubles back onto itself), it still does not help for clipping purposes.
I suspect that all you have to do is cut out at least one of the moveto segments (one in the loop, if not the one after it). You can then simplify the quick enumeration of the loop using an array instead of using indices and cut out the tracking of the "last drawn point".
Also, the correct index type in NSArray is NSUInteger , not int . Keep your types consistent - this helps to avoid pain later on the road.