What is the difference between closing the bezier path using closePath function and closing it manually?

≡放荡痞女 提交于 2019-12-03 13:46:50

The difference is that the [closePath] method actually adds an additional path element to the underlying CGPath that backs the UIBezierPath.

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.

This is particularly important when using the [containsPoint:] method of a UIBezierPath from the docs:

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.

I tried your example and indeed this happens with both UIBezierPath and with simple drawing on context with CGContextAddLineToPoint.

Can't answer your question, but it seems that adding

bpath.lineCapStyle = kCGLineCapSquare;

solves this exact issue. ( or the CGcontext... alternative ).

Probably closePath takes into account the lineWidth and other line parameters to form a properly closed polygon and adjusts the path itself to be closed nicely. This becomes more probable as you delete your upper right corner ( to make it a triangle ) and notice the linecapstyle won't work anymore, only the closePath gives you a nice triangle.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!