如何用渐变色的控制点填充贝塞尔曲线路径

时间:2018-06-25 08:24:25

标签: ios uibezierpath cagradientlayer

我需要用渐变色填充贝塞尔曲线路径。但是效果不是我所期望的,我知道这是由绘制方向引起的。

https://i.stack.imgur.com/uVYmK.png

如何获得第二张照片的效果?

https://i.stack.imgur.com/IIIx5.png

1 个答案:

答案 0 :(得分:0)

我的贝塞尔路径代码

        CGFloat deltaX = lineChartPoint.x - previousLineChartPoint.x;
        CGFloat controlPointX = previousLineChartPoint.x + (deltaX / 2);

        CGPoint controlPoint1 = CGPointMake(controlPointX, previousLineChartPoint.y);
        CGPoint controlPoint2 = CGPointMake(controlPointX, lineChartPoint.y);

        [bezierPath addCurveToPoint:CGPointMake(lineChartPoint.x, lineChartPoint.y) controlPoint1:controlPoint1 controlPoint2:controlPoint2];

我的gradientLayer代码

self.gradientLayer = [CAGradientLayer layer];
    self.gradientLayer.frame = CGRectMake(kVPadding, 0, CGRectGetWidth(self.bounds) - 2 * kVPadding, CGRectGetHeight(self.bounds));
    self.gradientLayer.colors = @[(__bridge id)[UIColor colorWithHex:0x8362FC alpha:0.9].CGColor,(__bridge id)[UIColor colorWithHex:0x517DF7 alpha:0.1].CGColor];
    self.gradientLayer.locations=@[@0.0,@1.0];
    self.gradientLayer.startPoint = CGPointMake(0.0,0.0);
    self.gradientLayer.endPoint = CGPointMake(0.0,1);
    [self.layer addSublayer:self.gradientLayer];

    self.gradientShapeLayer = [[CAShapeLayer alloc] init];
    self.gradientLayer.mask = self.gradientShapeLayer;
    self.gradientShapeLayer.path = bezierPath.CGPath;

@Dragonthoughts