2013-05-01 2 views
0

나는의 drawRect 구현이 있습니다CGContext 렌더링 유지하지 않는 선은

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    if (self.colorToRender) 
    { 
     CGContextSetStrokeColorWithColor(ctx, self.colorToRender.CGColor); 
    } 
    else 
    { 
     CGContextSetStrokeColorWithColor(ctx,[UIColor whiteColor].CGColor); 
    } 

    CGContextBeginPath(ctx); 
    CGContextMoveToPoint(ctx, self.center.x, self.center.y); 
    CGContextAddLineToPoint(ctx, self.pointToRender.x, self.pointToRender.y); 
    CGContextStrokePath(ctx); 
} 

이 라인마다 렌더링하지만 렌더링 라인이 화면에 남아 있지 않습니다.

이전에 렌더링 된 선을 유지하면서 화면에 새 선을 렌더링하는 방법은 무엇입니까?

+1

대부분의 경우보기의 clearsContextBeforeDrawing 플래그가 (이 기본이며, 또한 기본적으로 XIB에 체크되어) 설정 -> http://developer.apple.com/library/ios/documentation/UIKit/ 참조/UIView_Class/UIView/UIView.html # // apple_ref/occ/instp/UIView/clearsContextBeforeDrawing – borrrden

답변

0

drawRect는 뷰를 그릴 때마다 호출됩니다. 여러 줄을 그리려면 drawRect 메서드에서 각 줄을 다시 그려야합니다.

줄을 추가 할 때 drawRect 메서드에서 사용할 수있는 배열에 줄을 추가하여 그릴 줄을 결정하십시오.

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    if (self.colorToRender) 
    { 
     CGContextSetStrokeColorWithColor(ctx, self.colorToRender.CGColor); 
    } 
    else 
    { 
     CGContextSetStrokeColorWithColor(ctx,[UIColor whiteColor].CGColor); 
    } 


    // For each line you want to draw: 
    for (MyLine *thisLine in myLines) { 
     CGContextMoveToPoint(ctx, thisLine.startPoint.x, thisLine.startPoint.y); 
     CGContextAddLineToPoint(ctx, thisLine.endPoint.x, thisLine.endPoint.y); 
     CGContextStrokePath(ctx); 
    } 

} 
+0

각 행마다 setNeedsDisplay를 호출 중이며 이제는 initWithFrame보기에서 clearContextBeforeDrawing을 NO로 설정했습니다. 나는 같은 결과를 얻는다. 나는 BiezerPath를 사용하는 것을 끝내었고 더 잘 작동하는 것처럼 보인다. – jarryd

+0

@ Helium3 'initWithCoder :'가 아닌 (즉 XIB에서) 호출 된 것이 확실합니까? – borrrden

+0

코드에서 UIView를 만듭니다. – jarryd