0

I'm currently drawing an angular gradient using a method similar to the one shown by Rob's answer here. I have added an extension to CGContext to draw an angular gradient and would like it to handle the clipping path, so that callers can treat it similar to a method like fillPath(using:).

I have tried using func pathContains(CGPoint, mode: CGPathDrawingMode) on CGContext and func contains(CGPoint, using: CGPathFillRule, transform: CGAffineTransform) on the CGPath returned from the context.

Both of these appear to not use the clipping path, so my drawing goes outside the clipping area.

Is there a way to get either the path clipped already or a copy of the current clipping path so I can also check whether the pixel is contained inside that path also?

Daniel Tull
  • 1,736
  • 13
  • 19

1 Answers1

0

You should be able to check if the point is inside your unclipped path and check if the point is inside the clipping path. The clipped path is the intersection fo the clipping path and your path, so a point is inside the clipped path only it its inside both your unclipped path and inside the clipping path.

Josh Homann
  • 15,933
  • 3
  • 30
  • 33
  • 1
    That I understand, but I can't see any way to retrieve the clipping path from the CGContext. – Daniel Tull Nov 27 '17 at 15:36
  • Aren't you setting the clipping path? if not its the view's bounds. – Josh Homann Nov 27 '17 at 15:37
  • I am setting it but at a different level to where I need it here. There's good reasons for that, which include having multiple fill methods go through the same clipping code – so often I won't be drawing an angular gradient, but maybe just a solid fill. Also, some of the clipping paths are compound, so I never actually know what the clipping path is when I set it. (I set one path then add another and the context takes care of doing the correct thing when clipping using the even/odd rule.) – Daniel Tull Nov 27 '17 at 15:53