参考已接受的答案here我尝试实施此问题并遇到CAShapeLayer
路径问题我的代码如下:
-(void)drawGradientArc{
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
CAShapeLayer *circleLayer = [CAShapeLayer new];
circleLayer.lineWidth = 25;
circleLayer.fillColor = [UIColor clearColor].CGColor;
circleLayer.strokeColor = [UIColor redColor].CGColor;
CGFloat radius = 150;
[bezierPath addArcWithCenter:self.view.center radius:radius startAngle:0 endAngle:M_PI clockwise:YES];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.startPoint = CGPointMake(0,0);
gradientLayer.endPoint = CGPointMake(1,0);
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:circleLayer];
circleLayer.path = bezierPath.CGPath;
[self.view.layer addSublayer:circleLayer];
}
这将正确绘制半圆弧,但渐变的路径未正确设置,我无法拼凑出如何使用渐变来准确配置代码。有一个CGPathCreateCopyByStrokingPath
我认为我可能还需要调用以正确设置渐变的路径,但不太确定如何。任何有关如何构建代码以使其工作的帮助将不胜感激。我还包括了我试图重现的渐变图像。 CAShapeLayer
和CAGradientLayer
方法是理想的。