Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple paths in CAShapeLayer

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?

like image 366
rajomato Avatar asked Apr 01 '12 18:04

rajomato


3 Answers

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;
like image 85
Kjuly Avatar answered Sep 28 '22 10:09

Kjuly


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
like image 42
Quoc Nguyen Avatar answered Sep 28 '22 08:09

Quoc Nguyen


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;
like image 26
amergin Avatar answered Sep 28 '22 10:09

amergin