简体   繁体   English

在CAShapeLayer图下方绘制渐变

[英]Draw gradient below a CAShapeLayer graph

I'm drawing a graph using a CGPath applied to a CAShapeLayer. 我正在使用应用于CAShapeLayer的CGPath绘制图形。 The graph itself is drawn just fine, but I want to add a gradient underneath it afterwards. 该图本身绘制得很好,但是我想在其后添加一个渐变。 My problem is that the path is closed with a straight line going from the last point to the first point (see below) – this would make a gradient fill look totally ridiculous. 我的问题是,路径是从最后一点到第一个点的直线闭合的(请参见下文)–这会使渐变填充看起来完全荒谬。

在此处输入图片说明

As far as I can see, the only way to circumvent this issue is to draw two additional lines: one from the last point of the graph to the bottom-right corner, and from there, another one to the bottom-left corner. 据我所知,解决此问题的唯一方法是画两条额外的线:一条从图形的最后一个点到右下角,然后再从那里到左下角。 This would close the path off nicely, but it would add a bottom line to the graph, which I don't want. 这样可以很好地关闭路径,但是会在图表上增加底线,这是我所不希望的。

If I were using CGContext, I could easily solve this by changing the stroke color to transparent for the last two lines. 如果使用的是CGContext,则可以通过将最后两行的笔触颜色更改为透明来轻松解决此问题。 However, with the code below, I don't see how that would be possible. 但是,使用下面的代码,我看不到这是怎么可能的。

CGMutablePathRef graphPath = CGPathCreateMutable();

for (NSUInteger i = 0; i < self.coordinates.count; i++) {
    CGPoint coordinate = [self.coordinates[i] CGPointValue];

    if (!i) {
        CGPathMoveToPoint(graphPath, NULL, coordinate.x, coordinate.y);
    } else {
        CGPathAddLineToPoint(graphPath, NULL, coordinate.x, coordinate.y);
    }
}

CAShapeLayer *graphLayer = [CAShapeLayer new];
graphLayer.path = graphPath;
graphLayer.strokeColor = [UIColor whiteColor].CGColor;
graphLayer.fillColor = [UIColor redColor].CGColor;

[self.layer addSublayer:graphLayer];

I hope you guys can help me out! 我希望你们能帮助我!

Update: You suggest that I could create a CAGradientLayer, and then apply the graph layer as its mask. 更新:您建议我可以创建一个CAGradientLayer,然后将图形图层用作其蒙版。 I don't see how that would work, though, when the graph/path looks the way it does. 但是,当图形/路径看起来像它的方式时,我看不到它将如何工作。 I have replaced the image above with another graph that hopefully illustrates the problem better (note that I've given the CAShapeLayer a red fill). 我将上面的图像替换为另一个希望可以更好地说明问题的图形(请注意,我给CAShapeLayer填充了红色)。 As I see it, if I were to apply above layer as the mask of a CAGradientLayer, some of the gradient would lie above the graph, some it below. 如我所见,如果我将上面的图层应用为CAGradientLayer的蒙版,则某些渐变将位于图形上方,而某些渐变将位于图形下方。 What I want is for all of the gradient to be placed right beneath the graph. 我想要的是将所有渐变都放置在图形的正下方。

Maybe I'm not understanding the problem correctly, but if you're looking to add a consistent gradient, couldn't you create a gradient layer and then make your graphLayer be that layer's mask? 也许我没有正确理解问题,但是如果您要添加一致的渐变,是否不能创建渐变图层,然后使graphLayer成为该图层的蒙版?

Figure out whatever the min max bounds of your coordinates, create a CAGradientLayer that size, configure it however you might like and then, apply your graphLayer as it's mask. 找出坐标的最小最大界限,创建一个该大小的CAGradientLayer,根据需要进行配置,然后将graphLayer用作遮罩。 Then add the new CAGradientLayer to your self.layer. 然后将新的CAGradientLayer添加到您的self.layer中。

CAGradientLayer *gradientLayer = [CAGradientLayer layer];
// ... Configure the gradientLayer colors / locations / size / etc...
gradientLayer.mask = graphLayer;

[self.layer addSubLayer:gradientLayer];

This doesn't take into account the stroke, but it shouldn't be difficult to apply that as well if that's important. 这没有考虑到笔划,但是如果很重要的话,应用它也不应该很困难。

I solved the problem by creating two separate paths: One for the graph (as shown in my original post), and one that starts in the lower-right corner, moves in a straight line to the lower-left corner, and from there follows the same path as the graph. 我通过创建两个单独的路径解决了这个问题:一个用于图形(如我的原始文章中所示),另一个从右下角开始,一直直线移动到左下角,然后从那里开始与图形相同的路径。 By doing so, the path gets closed off nicely, since the graph ends at the same x-coordinate as where the path started. 这样,由于图形在与路径开始处相同的x坐标处结束,因此可以很好地关闭路径。

From there, I applied the second path to a CAShapeLayer, and then used this layer as the mask of a gradient layer. 从那里,我将第二条路径应用于CAShapeLayer,然后将其用作渐变层的蒙版。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM