使用closePath函数关闭贝塞尔曲线路径并手动关闭它有什么区别? [英] What is the difference between closing the bezier path using closePath function and closing it manually?
问题描述
我正在尝试使用 UIBezierPath
制作一个矩形。我采用了两种不同的方法来绘制它。另外,我将笔画宽度增加到25像素。
I am trying to make a rectangle using UIBezierPath
. I adopted two different ways to draw it. Also, I increased the stroke width to 25 px.
第一种方法:使用closePath
First method : Using closePath
UIBezierPath *bpath = [UIBezierPath bezierPath];
[bpath moveToPoint:CGPointMake(x, y)];
[bpath addLineToPoint:CGPointMake(x + w, y)];
[bpath addLineToPoint:CGPointMake(x + w, y + h)];
[bpath addLineToPoint:CGPointMake(x, y + h)];
[bpath closePath];
输出:
第二种方法:手动关闭路径
Second method : Closing the path manually
UIBezierPath *bpath = [UIBezierPath bezierPath];
[bpath moveToPoint:CGPointMake(x, y)];
[bpath addLineToPoint:CGPointMake(x + w, y)];
[bpath addLineToPoint:CGPointMake(x + w, y + h)];
[bpath addLineToPoint:CGPointMake(x, y + h)];
[bpath addLineToPoint:CGPointMake(x, y)];
输出:
在的文档中closePath
它说此方法通过在子路径中的第一个点和最后一个点之间创建一个线段来关闭当前子路径。此方法随后将当前点更新为新创建的线段的末尾,这也是现在关闭的子路径中的第一个点。
在第二种方法中,我创建了第一个和最后一个点之间的线段。那么,为什么第二个方法矩形没有被完全描边?
And in second method I am creating the line segment between first and last points. So, why in the second method rectangle is not completely stroked?
注意:这些方法之间的差异仅在笔划宽度显着增加时才可见。
Note: Difference between these methods is only visible when stroke width is increased significantly.
推荐答案
区别在于 [closePath]
方法实际上为底层添加了一个额外的路径元素支持UIBezierPath的CGPath。
The difference is that the [closePath]
method actually adds an additional path element to the underlying CGPath that backs the UIBezierPath.
如果你使用 [closePath]
,那么额外的类型为
将被追加到该最后一个线段之后的路径末尾。 kCathElementCloseSubpath
的CGPathElement
If you use [closePath]
, then an additional CGPathElement
with a type of kCGPathElementCloseSubpath
will be appended to the end of the path immediately after that last line segment.
当从文档中使用 UIBezierPath
的[containsPoint:]方法时,这一点尤其重要:
This is particularly important when using the [containsPoint:] method of a UIBezierPath
from the docs:
如果某个点在
内是一个开放的子路径,则该路径不被视为封闭,无论该区域是否在填充操作期间被绘制为
。因此,要确定打开的
路径上的鼠标命中,您必须创建路径对象的副本,并在调用此方法之前显式关闭
任何子路径(使用closePath方法)。
A point is not considered to be enclosed by the path if it is inside an open subpath, regardless of whether that area would be painted during a fill operation. Therefore, to determine mouse hits on open paths, you must create a copy of the path object and explicitly close any subpaths (using the closePath method) before calling this method.
这篇关于使用closePath函数关闭贝塞尔曲线路径并手动关闭它有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!