CAShapeLayer中的多个路径

raj*_*ato 14 cocoa cocoa-touch core-animation core-graphics ios

我想知道是否有可能让CAShapeLayer描边超过一条路径,因为它实际上只需要一条路径作为参数:

CAShapeLayer* myLayer=[CAShapeLayer layer];
myLayer.path=myBezierPath
Run Code Online (Sandbox Code Playgroud)

但是如果我想在这一层上划出多条路径呢?

Kju*_*uly 14

使用

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.
);
Run Code Online (Sandbox Code Playgroud)

代码示例:

CGMutablePathRef combinedPath = CGPathCreateMutableCopy(path.CGPath);
CGPathAddPath(combinedPath, NULL, path2.CGPath);
CGPathAddPath(combinedPath, NULL, path3.CGPath);
CGPathAddPath(combinedPath, NULL, path4.CGPath);
myLayer.path = combinedPath;
Run Code Online (Sandbox Code Playgroud)


ame*_*gin 6

另一种方法是追加这样的路径......

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;
Run Code Online (Sandbox Code Playgroud)


Quo*_*yen 5

@Kjuly的答案Swift 4.1版本

Swift中,您可以使用CGMutablePath

代码示例:

let combinedPath = CGMutablePath()
combinedPath.addPath(path1)
combinedPath.addPath(path2)
....
combinedPath.addPath(path_n)

myLayer.path = combinedPath
Run Code Online (Sandbox Code Playgroud)