开发者

Drawing a chart with gradient in Objective C

开发者 https://www.devze.com 2023-03-21 09:44 出处:网络
I\'m trying to make a simple app where a chart is drawn with a line and X axis. I want to fill parts of the view enclosed by chart and X axis with gradient. To fill them I use the following code

I'm trying to make a simple app where a chart is drawn with a line and X axis. I want to fill parts of the view enclosed by chart and X axis with gradient. To fill them I use the following code

        CGContextSaveGState(c);
        CGContextAddPath(c, CGContextCopyPath(c));
        CGContextClip(c);
        CGContextDrawLinearGradient(c, g, previousPointOfIntersection, intersectionPoint, 0);
        CGContextRestoreGState(c);

every time the line crosses the X axis. However, the problem is that the gradient fills the whole view between previous point of intersection and current one.

Is this a right way to draw a gradient for a part of view enclosed by lines?

I'd be very happy to hear any suggestions regarding my problem :)

P.S. here's the code of my class http://pastebin.com/wYiHk开发者_Python百科uVi


I'd say you don't have the path in the context that you think you do. If you've "stroked" the path for your graph it's been used up.

Replace your Add Path with:

  CGContextAddEllipseInRect(c, self.bounds);

to see if you get some clipping occurring.

If so, then you need to rebuild your path here instead of the AddPath/Ellipse code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号