如何将一些矩形UibezierPath对象加入一个?
我只是在代码中进行以下操作:
let path = UIBezierPath(rect: blurView.bounds)
path.usesEvenOddFillRule = true
path.append(UIBezierPath(rect: CGRect(x: 100, y: 100, width: 100, height: 100)))
path.append(UIBezierPath(rect: CGRect(x: 150, y: 150, width: 100, height: 100)))
//here you can add more paths, but the number is not known
let layer = CAShapeLayer()
layer.path = path.cgPath
layer.fillRule = .evenOdd
blurView.layer.mask = layer
效果如下:
但是我需要的只是将两个矩形的区域结合在一起,而不是排除永恒的区域。是否可以?
I simply do the following in code:
let path = UIBezierPath(rect: blurView.bounds)
path.usesEvenOddFillRule = true
path.append(UIBezierPath(rect: CGRect(x: 100, y: 100, width: 100, height: 100)))
path.append(UIBezierPath(rect: CGRect(x: 150, y: 150, width: 100, height: 100)))
//here you can add more paths, but the number is not known
let layer = CAShapeLayer()
layer.path = path.cgPath
layer.fillRule = .evenOdd
blurView.layer.mask = layer
and the effect is following:
Two rectangles overlapping one another. But all I need is to combine area from both rectanges, not to exclude everlapping area. Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用“偶数”填充规则非常适合在路径中“切一个孔”。但是,此代码:
如您所见,将是有问题的。
根据您要做的一切,您 可以使用库,例如,您可以通过布尔动作来操纵路径。
或者,您可以使用像这样的自定义
calayer
将多个路径倒入“切割蒙版”:要显示在使用中,我们将使用此图像:
在标准
uiimageView
中,用BluruivisualeffectView
覆盖,然后使用basic -cutoutlayer
类,带有两个重叠的rects作为Blur View lays layer Mask:在之前 :施加面具,看起来像这样:
应用掩模后,我们得到:
如我们所见,“重叠”显示。
那是一个非常简单的基本示例。对于一个更高级的示例,请看一下:
以及一个子分类
uivisualeffectView
为了方便起见:和一个示例控制器:
运行时,此示例将循环到重叠的rect,重叠的椭圆形,一些不同的宽度行, ,还有一些各种形状(只是为了给出一个想法):
Using the "even-odd" fill rule is great for "cutting a hole" in a path. However, this code:
will be, as you've seen, problematic.
Depending on what all you are wanting to do, you could use a library such as ClippingBezier which allows you to manipulate paths with boolean actions.
Or, you can use a custom
CALayer
like this to "invert" multiple paths to use as a "cutout mask":To show it in use, we'll use this image:
In a standard
UIImageView
, overlaid with a blurUIVisualEffectView
, and then use theBasicCutoutLayer
class with two overlapping rects as the blur view's layer mask:Before applying the mask, it looks like this:
after applying the mask we get:
As we see, the "overlap" displays as we want.
That was a very simple, basic example. For a more advanced example, take a look at this:
along with a subclassed
UIVisualEffectView
for convenience:and an example controller:
When run, this example will cycle through overlapping rect, overlapping ovals, some varying width lines, and some assorted shapes (just to give an idea):
我会选择 clippingbezier 因为它很快,易于使用且整洁。它将是这样的:
输出:
ed ,
似乎在使用uibezierpath时,这种方法似乎无法正常工作:)。为了克服这一点,这是我们可以构建自己的功能来做到这一点的方式:
我们还可以将上述解决方案扩展到多个路径。这是创建多个路径联合的一种方法:
这是一个示例
:
I would go with ClippingBezier because it is fast, easy to use and neat. It'll be something like this:
Output:
EDIT
It appears that this method doesn't work properly when using UIBezierPath(roundedRect:cornerRadius:). To overcome that, here is how we can construct our own func to do that:
We can also extend the above-mentioned solution to multiple paths. Here is one way to create the union of multiple paths:
Here is an example:
And the output: