Make independent copy of UIBezierPath?

Viewed 4029

I have a complex UIBezierCurve which I need to draw once with some particular line parameters, and then draw it again as overlay with other line parameters, but also I need the last part of the curve to be slightly shorter than in previous one.

To do this I want to create the curve by addLineToPoint:, moveToPoint: up to the last part, then make a copy of this curve and add the final segments of the line differently in original and copied curves. And then I stroke the original curve, and the copied one.

The problem is that it doesn't work as I expected. I create a copy of the curve by:

UIBezierPath* copyCurve = [originalCurve copy];

And the drawing which I do in the originalCurve after that, is applied also to the copyCurve, so I can't do independent drawing for any of this curves.

What is the reason for this connection between original and copy and how can I get rid of it?

EDIT 1: A solution which I've found is to create the copy in the following way:

UIBezierPath* copyCurve=[UIBezierPath bezierPathWithCGPath:CGPathCreateMutableCopy(originalCurve.CGPath)];

Since this works properly, maybe the problem is in the immutability of the copy I get with

[originalCurve copy]
3 Answers

copy() works fine for me as of Swift 4.

let copiedPath = originalPath.copy() as! UIBezierPath
copiedPath.addLine(...)

The originalPath does not get modified.

Related