如何从CALayer中切出一个缺口?

Sim*_*mon 5 iphone core-animation objective-c ios

我在旋转木马中有一组视图,每个视图都使用CAGradientLayer作为背景.旋转木马坐在带纹理的背景上.我被要求提供背景,以三角形显示所选视图.我不能只使用带有背景纹理的三角形图像,因为它不一定与主背景匹配.我想从当前视图的背景中切出一个凹口,以便通过凹口可以看到带纹理的背景.

我该怎么办呢?是否可以制作多边形层?

Sim*_*mon 10

我发现我能够使用CAShapeLayer来做到这一点:

CAShapeLayer *mask = [[[CAShapeLayer alloc] init] autorelease];
mask.frame = backgroundLayer.bounds;
mask.fillColor = [[UIColor blackColor] CGColor];

CGFloat width = backgroundLayer.frame.size.width;
CGFloat height = backgroundLayer.frame.size.height;

CGMutablePathRef path = CGPathCreateMutable();

CGPathMoveToPoint(path, nil, 0, 0); 
CGPathAddLineToPoint(path, nil, width, 0);
CGPathAddLineToPoint(path, nil, width, height);
CGPathAddLineToPoint(path, nil, (width/2) + 5, height);
CGPathAddLineToPoint(path, nil, width/2, height - 5);
CGPathAddLineToPoint(path, nil, (width/2) - 5, height);
CGPathAddLineToPoint(path, nil, 0, height);
CGPathAddLineToPoint(path, nil, 0, 0);
CGPathCloseSubpath(path);

mask.path = path;
CGPathRelease(path);

backgroundLayer.mask = mask;
Run Code Online (Sandbox Code Playgroud)