2012-02-05 4 views
1

저는 Stanford CS193p 과정을 연구 중이며 그래프를 그려보고 있습니다. 축 그리기는 작동하지만 그래프 자체를 그릴 수는 없습니다. 그 메시지를받습니다. 그리려 할 때 메시지가 나타납니다.문제 해결 drawingRect() 메서드의 CoreGraphics 선

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

} 

답변

5

당신은 for 루프 외부 CGContextStrokePath(context);을 배치해야합니다. 그렇지 않으면 루프를 통과 할 때마다 새로운 경로가 만들어지며 실패합니다.

+0

감사합니다. 완벽하게 작동했습니다. – MaxGabriel

+0

그게 나를 위해 일한 .. –

2

당신은에 문제가하지 마십시오

CGRectMake(origin.x, origin.y, 250, -250) 

당신은 음의 높이를 지정!

+0

iOS의 좌표계는 왼쪽 상단부터 시작합니다. 따라서 양수의 x 값은 오른쪽으로, 양의 y 값은 아래로 내려갑니다. 따라서 라인을 올리려면 CGRectMake의 height 인수에 음의 y 값을 사용합니다. – MaxGabriel

+1

선을 그 으려면 참이지만이 절차 (CGRectMake)는 직사각형을 정의합니다. – aleene

+1

고마워요, 이것은 실제로 중요한 버그 수정으로 밝혀졌습니다. – MaxGabriel