ios – 使用closePath函数关闭贝塞尔曲线路径并手动关闭它有什么区别?

我正在尝试使用UIBezierPath创建一个矩形.我采用了两种不同的方法来绘制它.另外,我将笔画宽度增加到25像素.

第一种方法:使用closePath

UIBezierPath *bpath = [UIBezierPath bezierPath];

[bpath movetoPoint:CGPointMake(x,y)];
[bpath addLinetoPoint:CGPointMake(x + w,y + h)];
[bpath addLinetoPoint:CGPointMake(x,y + h)];
[bpath closePath];

输出

第二种方法:手动关闭路径

UIBezierPath *bpath = [UIBezierPath bezierPath];

[bpath movetoPoint:CGPointMake(x,y)];

输出

在closePath的文档中,它说这个方法通过在子路径中的第一个和最后一个点之间创建一个线段来关闭当前子路径.此方法随后将当前点更新为新创建的线段的末尾,这也是现在关闭的子路径中的第一个点.

在第二种方法中,我创建了第一个和最后一个点之间的线段.那么,为什么第二种方法矩形没有被完全描边?

注意:这些方法间的差异仅在笔划宽度显着增加时才可见.

解决方法

区别在于[closePath]方法实际上向支持UIBezierPath的底层CGPath添加一个额外的路径元素.

如果使用[closePath],则会将另一个类型为kCGpathelementCloseSubpath的CGpathelement追加到该最后一个线段之后的路径末尾.

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

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.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...