How to fill a UIBezierPath with a gradient?

后端 未结 1 1213
故里飘歌
故里飘歌 2020-11-28 11:42

I\'ve drawn a graph using a UIBezierPath. I can fill the area under the graph with a solid color but I want to fill the area under the graph with a gradient rather than with

相关标签:
1条回答
  • 2020-11-28 12:03

    The easiest approach is to use the original graph bezier path to help you construct a mask and impose this mask on a gradient layer. Now impose the graph layer on top of the gradient layer.

    So, for example, make a CAShapeLayer, close the graph bezier path, set its path as the shape layer's path, and fill it black. Now you have a mask that is the shape of the area under the graph. Now make a CAGradientLayer and make the CAShapeLayer its mask. In front of that, place the actual graph.

    So for example (EDITED):

    enter image description here

    Here's the code I used to create that drawing (my bezier path is very simple, just four points joined by three lines, but you can see clearly that the area under it is a 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];
    
    0 讨论(0)
提交回复
热议问题