Reputation: 12292
Say you have a UIBez you have built:
Now I want to "clip" it, perhaps with this rectangle:
So in the example the final path I want would be:
(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
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):
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