问题
I want to draw a Circle with Letter punched out of it. to do this, I need to stroke the circle clockwise and the letter counterclockwise.
That is all good and well, but when I get the letter path using Core Text I can't figure out how to essentially reverse the path. NOT MIRROR or ROTATE or anything...that is straightforward enough. I want the point stroke order to be counterclockwise.
it is actually hilariously difficult. I've had entirely too many visual 'off by one' errors.
回答1:
About all I can think of is to call CGPathApply on the path and save information about all of the points, and then redraw the path in the opposite direction. Remember to call CGPathCloseSubpath
when you're done.
回答2:
iOS 6 introduced -bezierPathByReversingPath
It had some issues at first but it seems to be the answer to this question.
回答3:
I know this question is as old as dirt, but, I recently needed to reverse a CGPath. There are absolutely no examples on how to do this, and I needed this to work on Mac as well as iOS, so using -bezierPathByReversingPath
was out of the question.
So here is the code to reverse a CGPath. This works with open paths as well as closed. It handles complex paths as well as simple paths. It also handles paths with Cubic bezier elements, Quadratic bezier elements as well as lines. I can't post code because it's actually a couple of simple classes, so I had to Zip them.
There are two C++ classes in the attached Zip file. One is the CCGPathReverse
class, the other is a class CCGPathIterator
class which is very similar to GDI+'s GraphicsPathIterator
(it is used by the CCGPathReverse
class.)
Here is a link to the classes.
To use it, just include CGPathReverse.h
header and call it as so:
CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddRect(myPath, NULL, CGRectMake(0,0,200,200));
CCGPathReverse r(myPath);
CGPathRelease(myPath);
myPath = r.GetReversedPathMutableCopy();
//
// myPath is now a counter-clockwise rectangle, do what you want with it...
//
CGPathRelease(myPath);
I hope this helps someone out there, because reversing a CGPath isn't trivial!
来源:https://stackoverflow.com/questions/7681983/how-to-reverse-the-point-order-of-a-cgpath