首页 > 解决方案 > UIBezierPath - 你能“剪辑”一个吗?

问题描述

假设您已经构建了一个 UIBez:

在此处输入图像描述

现在我想“剪辑”它,也许用这个矩形:

在此处输入图像描述

因此,在示例中,我想要的最终路径是:

在此处输入图像描述

(或者在示例中,您可以在右侧“减去”一个矩形,在底部“减去”一个。)

iOS 工具包是否包含用于 UIBezierPath 构造的此功能?

我认为答案是“不”,但这是无法通过谷歌搜索到的事情之一,因为您获得了不相关的 QA。所以我找不到答案。

请注意- 这与简单地“添加一个洞”完全不同,因为这是一种司空见惯的使用.append方式(例如https://stackoverflow.com/a/57514286/294884

标签: iosuibezierpath

解决方案


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 “<a href="https://stackoverflow.com/search?q=uibezierpath%20intersection">UIBezierPath intersection”, you will see some relevant examples. Even then, you’ll have interesting edge cases about how to algorithmically close your paths.


推荐阅读