Ios 如何用渐变填充UIBezierPath?

Ios 如何用渐变填充UIBezierPath?,ios,uibezierpath,Ios,Uibezierpath,我用UIBezierPath绘制了一个图形。我可以用纯色填充图形下的区域,但我想用渐变填充图形下的区域,而不是用纯色。但我不确定如何使梯度只适用于图形,而不适用于整个视图,我读了一些问题,但没有发现任何适用的 这是主要的图形绘制代码: // Draw the graph UIBezierPath *barGraph = [UIBezierPath bezierPath]; barGraph.lineWidth = 1.0f; [blueColor setStroke]; TCDataPoint

我用UIBezierPath绘制了一个图形。我可以用纯色填充图形下的区域,但我想用渐变填充图形下的区域,而不是用纯色。但我不确定如何使梯度只适用于图形,而不适用于整个视图,我读了一些问题,但没有发现任何适用的

这是主要的图形绘制代码:

// Draw the graph
UIBezierPath *barGraph = [UIBezierPath bezierPath];
barGraph.lineWidth = 1.0f;
[blueColor setStroke];
TCDataPoint *dataPoint = self.testData[0];
CGFloat x = [self convertTimeToXPoint:dataPoint.time];
CGFloat y = [self convertDataToYPoint:dataPoint.dataUsage];
CGPoint plotPoint = CGPointMake(x,y);
[barGraph moveToPoint:plotPoint];
for (int ii = 1; ii < [self.testData count]; ++ii)
{
    dataPoint = self.testData[ii];
    x = [self convertTimeToXPoint:dataPoint.time];
    y = [self convertDataToYPoint:dataPoint.dataUsage];
    plotPoint = CGPointMake(x, y);
    [barGraph addLineToPoint:plotPoint];
}
[barGraph stroke];

最简单的方法是使用原始图形bezier路径来帮助您构建遮罩,并将此遮罩施加到渐变层上。现在将图形层施加在渐变层的顶部

例如,制作一个CAShapeLayer,关闭图形bezier路径,将其路径设置为形状层的路径,并将其填充为黑色。现在有了一个遮罩,它是图形下区域的形状。现在制作一个CAGradientLayer,并将CAShapeLayer设置为其
掩码
。在前面,放置实际的图形

例如(编辑的):

下面是我用来创建该图形的代码(我的bezier路径非常简单,只有四个点由三条线连接,但您可以清楚地看到它下面的区域是渐变):

CAShapeLayer*shape=[[CAShapeLayer alloc]init];
shape.frame=self.graph.bounds;
CGH=形状.框架.尺寸.高度;
CGFloat w=形状.框架.尺寸.宽度;
NSArray*点=@[
[NSValue VALUE WITH CGPOINT:CGPointMake(0,h-50)],
[NSValue VALUE WITH CGPOINT:CGPointMake(70,h-100)],
[NSValue VALUE WITH CGPOINT:CGPointMake(140,h-75)],
[NSValue VALUE WITH CGPOINT:CGPointMake(w,h-150)],
];
UIBezierPath*p=[UIBezierPath新建];
[p moveToPoint:[points[0]CGPointValue]];
对于(NSInteger i=1;i
另请参见此:和此:谢谢,我将尝试您的建议。在我发布的代码中,为什么对CGContectClip的调用不将渐变的后续绘制限制为图形路径?@Reza.Ab绘制渐变并模糊它。这将是一个不同的问题。你可以达到任何效果,你知道如何绘制。这个问题不是关于任何特定的效果(尽管渐变是作为示例使用的,因为它是OP想要的),而是关于如何使该效果仅出现在由bezier路径定义的区域。@Reza.Ab您只是让我重复我自己(在两个不同的地方)。反复说同样的话不会改变任何事情。我已经回答了这个问题。试试看!
[barGraph closePath];

CGFloat colors [] = {
    1.0, 0.0, 0.0, 1.0,
    1.0, 1.0, 1.0, 1.0
};

CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colors, NULL, 2);
CGColorSpaceRelease(baseSpace);

CGPoint startPoint = CGPointMake(CGRectGetMidX(self.bounds), self.topY);
CGPoint endPoint = CGPointMake(CGRectGetMidX(self.bounds), self.bottomY);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGGradientRelease(gradient);
CAShapeLayer* shape = [[CAShapeLayer alloc] init];
shape.frame = self.graph.bounds;
CGFloat h = shape.frame.size.height;
CGFloat w = shape.frame.size.width;

NSArray* points = @[
                    [NSValue valueWithCGPoint:CGPointMake(0,h-50)],
                    [NSValue valueWithCGPoint:CGPointMake(70,h-100)],
                    [NSValue valueWithCGPoint:CGPointMake(140,h-75)],
                    [NSValue valueWithCGPoint:CGPointMake(w,h-150)],
                    ];

UIBezierPath* p = [UIBezierPath new];
[p moveToPoint:[points[0] CGPointValue]];
for (NSInteger i = 1; i < points.count; i++)
    [p addLineToPoint:[points[i] CGPointValue]];

shape.path = p.CGPath;
shape.strokeColor = [UIColor blackColor].CGColor;
shape.lineWidth = 2;
shape.fillColor = nil;

CAGradientLayer* grad = [[CAGradientLayer alloc] init];
grad.frame = self.graph.bounds;
grad.colors = @[(id)[UIColor blueColor].CGColor,
                (id)[UIColor yellowColor].CGColor];

CAShapeLayer* mask = [[CAShapeLayer alloc] init];
mask.frame = self.graph.bounds;
[p addLineToPoint:CGPointMake(w,h)];
[p addLineToPoint:CGPointMake(0,h)];
[p closePath];
mask.path = p.CGPath;
mask.fillColor = [UIColor blackColor].CGColor;
grad.mask = mask;

[self.graph.layer addSublayer:grad];
[self.graph.layer addSublayer:shape];