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?
Use
UIBezierPath. You can add new segments by callingaddLineToPoint.