Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making UIBezierPath more like NSBezierPath with elementCount and elementAtIndex

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 subclass 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?

like image 885
EFC Avatar asked Jul 06 '11 17:07

EFC


2 Answers

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.

like image 63
Seivan Avatar answered Oct 21 '22 08:10

Seivan


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.

like image 28
Pieter Avatar answered Oct 21 '22 06:10

Pieter