我正在研究斯坦福CS193p课程并试图绘制图表。绘制轴有效,但我无法绘制图形。我收到了消息
CGContextAddLineToPoint: no current point.
当我尝试画画时。这是代码。
- (void)drawRect:(CGRect)rect
{
NSLog(@"Drawing Graph View");
CGPoint origin = CGPointMake(20, 350);
CGRect rect2 = CGRectMake(origin.x, origin.y, 250, -250);
[AxesDrawer drawAxesInRect:rect2 originAtPoint:origin scale:[self scale]];
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
CGContextSetLineWidth(context, 2);
[[UIColor redColor] setStroke];
CGContextMoveToPoint(context, origin.x, origin.y);
for (CGFloat i=0; i<250; i++) {
CGFloat yChange = [self.dataSource deltaY:self];
CGContextAddLineToPoint(context, origin.x+i, origin.y-yChange);
CGContextStrokePath(context);
}
UIGraphicsPopContext();
}
答案 0 :(得分:5)
您必须将CGContextStrokePath(context);
置于for
循环之外。否则它将在循环的每次运行中创建一个新路径,但失败。
答案 1 :(得分:2)
你没有遇到问题:
CGRectMake(origin.x, origin.y, 250, -250)
指定负高度!