Search code examples
objective-ccgpathquartz-core

QuartzCore: Draw a shape with a hole in it?


How would one draw a shape with a hole in it using QuartzCore?

Here is an example: enter image description here

I already understand how to draw a path:

CGMutablePathRef maskPath = CGPathCreateMutable();
CGPathMoveToPoint(maskPath, NULL, x1, y1);
CGPathAddLineToPoint(maskPath, NULL, x2, y2);
CGPathAddLineToPoint(maskPath, NULL, x3, y3);
CGPathCloseSubpath(maskPath);

and apply it to a UIView:

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = viewToMask.bounds;
maskLayer.path = maskPath;
viewToMask.layer.mask = maskLayer;

but I am not entirely sure how I could create a path with a hole in the center. Do I need to somehow subtract from the path, maybe?


Solution

  • Since you are setting the CGPath as the path property of a CAShapeLayer to be the mask, you should look at Crop a CAShapeLayer retrieving the external path and its answer.