iOS:CGContextStrokePath 不断更新路径

发布于 2024-12-12 03:08:26 字数 897 浏览 0 评论 0原文

我想构建一个小线图,当我调用 setPointInGraph: 方法时,该线图会得到更新。每次我调用它时,我都希望用我刚刚添加的点更新图表,因此从我的最后一个点画一条线。我在下面的代码中看到问题,在“drawRect:”中,它只是不断覆盖旧路径。

- (void)setPointInGraph:(float)p {

    self.point = p;
    [self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGFloat maxX = CGRectGetMaxX(rect);
    CGFloat maxY = CGRectGetMaxY(rect);

    CGColorRef strokeColor = [self.lineColor CGColor];
    CGContextSetStrokeColorWithColor(context, strokeColor);
    CGContextSetLineWidth(context, self.lineWidth);
    CGContextBeginPath(context);                
    CGContextMoveToPoint(context, 0.0, maxY - maxY * self.point);
    CGContextAddLineToPoint(context, maxX * (point / count), maxY - maxY * self.point);
    CGContextStrokePath(context);

    count++;

}

如何保留路径(例如属性)并在我选择时更新它?如何不断添加到此路径而不覆盖它?

I am wanting to build a small line graph that get updated when I call my setPointInGraph: method. Each time I call that, I want the graph to be updated with the point that I just added, and therefore draw a line from my last point. I see the problem in my code below, in 'drawRect:' that it just keeps overwriting the old path.

- (void)setPointInGraph:(float)p {

    self.point = p;
    [self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGFloat maxX = CGRectGetMaxX(rect);
    CGFloat maxY = CGRectGetMaxY(rect);

    CGColorRef strokeColor = [self.lineColor CGColor];
    CGContextSetStrokeColorWithColor(context, strokeColor);
    CGContextSetLineWidth(context, self.lineWidth);
    CGContextBeginPath(context);                
    CGContextMoveToPoint(context, 0.0, maxY - maxY * self.point);
    CGContextAddLineToPoint(context, maxX * (point / count), maxY - maxY * self.point);
    CGContextStrokePath(context);

    count++;

}

How can I keep the path around (such as a property) and update it when I choose to? How can I continually add to this path not overwrite it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留蓝 2024-12-19 03:08:26

使用UIBezierPath。您可以通过调用addLineToPoint来添加新线段。

Use UIBezierPath. You can add new segments by calling addLineToPoint.

情归归情 2024-12-19 03:08:26

不要在图形上下文中创建路径,而是创建一个常规路径,然后对其进行描边:

path = CGPathCreateMutable();
CGPathAddLineToPoint(path, ...);

CGContextBeginPath(context);
CGContextAddPath(path);
CGContextStrokePath(context);

当您需要更新路径时,创建前一个路径的可变副本,并向其中添加线条:

path = CGPathCreateMutableCopy(lastPath);
CGPathRelease(lastPath);
CGPathAddLineToPoint(path, ...);

然后再次绘制它:

CGContextBeginPath(context);
CGContextAddPath(path);
CGContextStrokePath(context);

Instead of creating a path in the graphics context, create a regular path which you then stroke:

path = CGPathCreateMutable();
CGPathAddLineToPoint(path, ...);

CGContextBeginPath(context);
CGContextAddPath(path);
CGContextStrokePath(context);

When you need to update the path, create a mutable copy of the previous path, and add the line to it:

path = CGPathCreateMutableCopy(lastPath);
CGPathRelease(lastPath);
CGPathAddLineToPoint(path, ...);

And then draw it again:

CGContextBeginPath(context);
CGContextAddPath(path);
CGContextStrokePath(context);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文