Reverse CGPath in Rect

I am looking for an inverse of CGPath (relative to Rect). By this, I mean that I defined a CGPath , and I want to get a CGPath that describes all the other areas in rect.

I experimented differently and for several days I tried to work for several days. I just can't solve it!

Consider the following rectangle with a given CGPath (where green is the specified CGPath filled):

enter image description here

Now, when I want (inverted CGPath ), you will get the following:

enter image description here

In particular, this is for use with SKCropNode and SKPhysicsBody in SpriteKit, however I create a polygon for the node mask using CGPaths (and saving the path in ivar).

Is it possible to change CGPath or some other way? If so, how?

Thanks in advance.

+6
source share
1 answer

I copied the code from the @DavidRonnqvist answer associated with it, but modified it to extract the UIBezierPath from CGPath, which you need:

 CGPath filledPath = ...; //This is the inner filled shape CAShapeLayer *invertedLayer = [CAShapeLayer layer]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithCGPath:filledPath]; [invertedLayer setPath:[maskPath CGPath]]; [invertedLayer setFillRule:kCAFillRuleEvenOdd]; [invertedLayer setFillColor:[[UIColor colorOfYourChoice] CGColor]]; 

I have not tested this, but theoretically it should work.

+5
source

Source: https://habr.com/ru/post/958266/


All Articles