微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

原文地址:https://www.jb51.cc/iOS/334489.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐