我正在尝试在CAShapeLayer上应用渐变色。为此我写代码,
-(void)addCircle{
{
// Drawing code
UIBezierPath *aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
radius:125
startAngle:0
endAngle:DEGREES_TO_RADIANS(180)
clockwise:NO];
CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
[shapeLayer setFrame: self.frame];
[shapeLayer setPath: [aPath CGPath]];
shapeLayer.lineWidth = 30.0f;
[shapeLayer setStrokeColor:[[UIColor redColor] CGColor]];
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
[shapeLayer setMasksToBounds:YES];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.startPoint = CGPointMake(0.5,1.0);
gradientLayer.endPoint = CGPointMake(0.5,0.0);
gradientLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
NSMutableArray *colors = [NSMutableArray array];
for (int i = 0; i < 10; i++) {
[colors addObject:(id)[[UIColor colorWithHue:(0.1 * i) saturation:1 brightness:.8 alpha:1] CGColor]];
}
gradientLayer.colors = colors;
[gradientLayer setMask:shapeLayer];
[self.layer addSublayer:shapeLayer];
}
但我没有像这样的渐变色输出,
我想要这样的最终输出,
但我的主要目标是在CAShapeLayer上应用渐变效果。在我给出的代码中我错了什么?
答案 0 :(得分:0)
主要问题是您要将shapeLayer
添加到视图的图层,但实际上您只想将其用作蒙版,因此您应该添加gradientLayer
。
下一个问题是CAGradientLayer
不支持像第二个屏幕截图中那样的径向渐变。你需要做自定义绘图才能达到这个效果(或者只是使用图像)。