closures使用closePath函数的贝塞尔path和手动closures之间有什么区别?

我正在尝试使用UIBezierPath做一个矩形。 我采取了两种不同的方式来绘制它。 另外,我增加了行程宽度为25像素。

第一种方法:使用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]; 

输出:

使用closePath

第二种方法:手动closurespath

 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的文档中,它说This method closes the current subpath by creating a line segment between the first and last points in the subpath. This method subsequently updates the current point to the end of the newly created line segment, which is also the first point in the now closed subpath. This method closes the current subpath by creating a line segment between the first and last points in the subpath. This method subsequently updates the current point to the end of the newly created line segment, which is also the first point in the now closed subpath.

在第二种方法中,我创build了第一个点和最后一个点之间的线段。 那么,为什么在第二个方法矩形没有完全抚摸?

注:这些方法之间的区别仅在笔画宽度显着增加时才可见。

不同之处在于[closePath]方法实际上是将其他path元素添加到支持UIBezierPath的底层CGPath中。

如果使用[closePath] ,那么types为kCGPathElementCloseSubpath附加kCGPathElementCloseSubpath将被追加到紧跟在最后一个线段之后的path末尾。

从文档中使用UIBezierPath的[containsPoint:]方法时,这一点尤为重要:

如果一个点位于一个打开的子path中,则该点不会被path所包围,无论该区域在填充操作过程中是否被绘制。 因此,要确定打开path上的鼠标命中,您必须创build一个path对象的副本,并在调用此方法之前显式closures任何子path(使用closePath方法)。

我尝试了你的例子,事实上这两个UIBezierPathCGContextAddLineToPoint上下文的简单绘图。

不能回答你的问题,但似乎添加

bpath.lineCapStyle = kCGLineCapSquare;

解决了这个确切的问题。 (或者CGcontext …替代)。

可能closePath会考虑lineWidth和其他线参数,以形成一个正确closures的多边形,并调整path本身以很好地closures。 当您删除右上angular(使其成为三angular形)并且注意linecapstyle将不再工作时,这将变得更加可能,只有closePath会给您一个很好的三angular形。