I was wondering if it is somehow possible to let a CAShapeLayer stroke more than only one path since actually it only takes one path as parameter like:
CAShapeLayer* myLayer=[CAShapeLayer layer];
myLayer.path=myBezierPath
But what if I would like to stroke more than one path onto that layer?
Use
void CGPathAddPath (
CGMutablePathRef path1, // The mutable path to change.
const CGAffineTransform *m, // A pointer to an affine transformation matrix, or NULL if no transformation is needed. If specified, Quartz applies the transformation to path2 before it is added to path1.
CGPathRef path2 // The path to add.
);
Code example:
CGMutablePathRef combinedPath = CGPathCreateMutableCopy(path.CGPath);
CGPathAddPath(combinedPath, NULL, path2.CGPath);
CGPathAddPath(combinedPath, NULL, path3.CGPath);
CGPathAddPath(combinedPath, NULL, path4.CGPath);
myLayer.path = combinedPath;
The Swift 4.1 version of @Kjuly's answer
In Swift You can use the CGMutablePath
Code example:
let combinedPath = CGMutablePath()
combinedPath.addPath(path1)
combinedPath.addPath(path2)
....
combinedPath.addPath(path_n)
myLayer.path = combinedPath
Another way of doing this is to append paths like so...
UIBezierPath *left_path = [UIBezierPath bezierPathWithRect:frame_left];
UIBezierPath *right_path = [UIBezierPath bezierPathWithRect:frame_right];
[left_path appendPath:right_path];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = left_path.CGPath;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With