将子图层渐变遮蔽到CAShapeLayer

时间:2014-06-03 11:58:13

标签: objective-c calayer

我有CAShapeLayer bezierPathWithOvalInRect。我想让渐变图层仅填充圆圈,但此刻它会填满整个框架。

我该怎么做?

CAShapeLayer *layerInsides = [CAShapeLayer layer];
layerInsides.path = [UIBezierPath bezierPathWithOvalInRect:rectFrom(frameInsides)].CGPath;

CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = layerInsides.frame;
gradientLayer.colors = @[(id)[UIColor colorWithRed:1 green:0 blue:0 alpha:1].CGColor, (id)[UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor];
gradientLayer.locations = @[@0.0, @1];
gradientLayer.cornerRadius = self.layer.cornerRadius;
[layerInsides addSublayer:gradientLayer];

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要将CAShapeLayer设置为彩色图层的mask,而不是将内容作为子图层添加到形状中:

gradientLayer.mask = layerInsides;