在 Objective C 中绘制带有渐变的图表
我正在尝试制作一个简单的应用程序,其中使用线条和 X 轴绘制图表。我想用渐变填充图表和 X 轴包围的视图部分。 我都会使用以下代码
CGContextSaveGState(c);
CGContextAddPath(c, CGContextCopyPath(c));
CGContextClip(c);
CGContextDrawLinearGradient(c, g, previousPointOfIntersection, intersectionPoint, 0);
CGContextRestoreGState(c);
为了填充它们,每次线穿过 X 轴时 。然而,问题是渐变填充了前一个交点和当前交点之间的整个视图。
这是为线条包围的视图部分绘制渐变的正确方法吗?
我很高兴听到有关我的问题的任何建议:)
PS 这是我的类的代码 http://pastebin.com /wYiHkuVi
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/wYiHkuVi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说的是,你在上下文中没有你认为的路径。
如果你“抚摸”了图表的路径,它就已经用完了。
将“添加路径”替换为:
以查看是否发生了一些剪辑。
如果是这样,那么您需要在此处重建路径而不是 AddPath/Ellipse 代码。
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:
to see if you get some clipping occurring.
If so, then you need to rebuild your path here instead of the AddPath/Ellipse code.