Fattie
Fattie

Reputation: 12292

UIBezierPath - can you "clip" one?

Say you have a UIBez you have built:

enter image description here

Now I want to "clip" it, perhaps with this rectangle:

enter image description here

So in the example the final path I want would be:

enter image description here

(Alternately in the example you could "subtract" a rectangle on the right and one on the bottom.)

Does the iOS toolkit include this ability for UIBezierPath construction?

I think the answer is "No" but it's one of those things that is impossible to google, because, you get unrelated QA. So I cannot find the answer.

Pls note - this is totally different from simply "adding a hole" as one does as a commonplace using .append (example of that https://stackoverflow.com/a/57514286/294884 )

Upvotes: 2

Views: 423

Answers (1)

Rob
Rob

Reputation: 438467

There is no built-in clipping of paths (and definitely no auto-closing of clipped paths, either).


Beyond the “adding a hole” technique that you’ve mentioned, the other approach is just masking/clipping it. And I know you know this, but for the sake of future readers, the built-in clipping/masking is only available within a graphics context (or the mask of a CALayer). For example, if you have a CAShapeLayer, you can set its mask (to the rect illustrated by the dashed rectangle below, for example):

enter image description here

Obviously, this approach can’t “close” the path(s) that have been clipped.

However, if your paths consist of a series of line segments, you theoretically could write your own routine to iterate through those line segments looking for intersections with your rectangular clipping mask (as well as detecting whether they’re contained by the clipping mask at all). If you search for “UIBezierPath intersection”, you will see some relevant examples. Even then, you’ll have interesting edge cases about how to algorithmically close your paths.

Upvotes: 2

Related Questions