无法在drawRect()方法中绘制CoreGraphics行

Max*_*iel 1 core-graphics cs193p drawrect ios

我正在研究斯坦福CS193p课程,我正在尝试绘制图表.绘制轴有效,但我无法绘制图形本身.我收到了消息

CGContextAddLineToPoint: no current point.
Run Code Online (Sandbox Code Playgroud)

当我试图画画.这是代码.

- (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();

}
Run Code Online (Sandbox Code Playgroud)

Ole*_*ann 5

你必须放在循环CGContextStrokePath(context);之外for.否则,它将在循环的每次运行中创建一个新的路径,并且失败.