开发者

Making UIBezierPath more like NSBezierPath with elementCount and elementAtIndex

开发者 https://www.devze.com 2023-03-18 17:17 出处:网络
When moving some Cocoa code to Cocoa Touch I was disappointed to find that UIBezierPath is missing the \"Accessing Elements of a Path\" methods:

When moving some Cocoa code to Cocoa Touch I was disappointed to find that UIBezierPath is missing the "Accessing Elements of a Path" methods:

– elementCount
– elementAtIndex:
– elementAtIndex:associatedPoints:
– removeAllPoints
– setAssociatedPoints:atIndex:

The only way to get at these elements in Cocoa Touch seems to be through CGPathApply. Before I try to recreate this as a subclas开发者_高级运维s or category of UIBezierPath, I was wondering if this had already been done. Does anyone have an idea if something like this is already available?


I made a port on https://github.com/seivan/UIBezierPathPort but it's with Swift. Has a test suite and documentation. Works as of Beta 5.

Technically you should be able to use Swift on a Obj-C project.

Let me know how you like it.


I've bumped into the same problem a couple of months ago and couldn't find anything readily available back then. (Truth be told, since going the CGPathApply route wasn't that bad for my needs, I didn't look very hard to be honest).

The applier function is called for every CGPathElement in the CGPath, such an element consists of a CGPathElementType and a C-array of CGPoints.

Since a CGPathElementType is an enum with only five different values

enum CGPathElementType {
   kCGPathElementMoveToPoint,
   kCGPathElementAddLineToPoint,
   kCGPathElementAddQuadCurveToPoint,
   kCGPathElementAddCurveToPoint,
   kCGPathElementCloseSubpath
};

You don't need to write that much code to do (control)point manipulation / inspection of a path. Having the same interface available would've been nice though.

0

精彩评论

暂无评论...
验证码 换一张
取 消