2013-05-07 2 views
1

사용자 정의 UIView이 있습니다. 손가락이 움직이면 선과 점을 계속 지킵니다.여러 경로가 화면에 유지되지 않습니다.

- (void)makeCGPath 
{ 
CGMutablePathRef path = CGPathCreateMutable(); 

if (lines&& lines.count>0) 
{ 
    for (int i = 0; i < lines.count; i++) 
    { 
     CGMutablePathRef linePath = CGPathCreateMutable(); 

     NSArray *tempArray = [lines objectAtIndex:i]; 
     CGPoint p = [[tempArray objectAtIndex:0]CGPointValue]; 
     CGPathMoveToPoint(linePath, NULL, p.x, p.y); 
     for (int j = 1; j < tempArray.count; j++) 
     { 
      p = [[tempArray objectAtIndex:j]CGPointValue]; 
      CGPathAddLineToPoint(linePath, NULL, p.x, p.y); 
     } 
     CGPathAddPath(path, NULL, linePath); 
     CGPathRelease(linePath); 



    } 
} 
if (points&& points.count > 0) 
{ 
    CGMutablePathRef pointPath = CGPathCreateMutable(); 
    CGPoint p = [[points objectAtIndex:0]CGPointValue]; 
    CGPathMoveToPoint(pointPath, NULL, p.x, p.y); 
    for (int i = 1; i < points.count;i++) 
    { 
     p = [[points objectAtIndex:i]CGPointValue]; 
     CGPathAddLineToPoint(pointPath, NULL, p.x, p.y); 
    } 
    CGPathAddPath(path, NULL, pointPath); 
    CGPathRelease(pointPath); 
} 


drawPath = CGPathCreateCopy(path); 
[self setNeedsDisplay]; 

[lines removeAllObjects]; 
[points removeAllObjects]; 
} 

을 그리고 내의 drawRect이 다음과 같이이다 : 그럼이 방법으로 CGPath에 포인트를합니다.

- (void)drawRect:(CGRect)rect 
{ 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(ctx, 3); 
[[UIColor blackColor]set]; 
CGContextAddPath(ctx, drawPath); 
CGContextStrokePath(ctx); 
} 

화면의 마지막 줄만 표시하는 것이 이상하게 보입니다. 그게 뭐가 잘못 됐어?

어떻게 이러한 경로를 모두 화면에 유지할 수 있습니까?

답변

3

나는 똑같은 문제가 있습니다. iOS 5.0에서는 작동하지 않지만 iOS 5.1 이상에서는 작동합니다. 나는 아직도 문제를 찾고있다.

UPADTE : UIBezierPath를 사용하여 문제가 해결되었습니다. 당신은 Besier 사용하여 모든 경로를 그릴 다음 CGPath에이 방법을 변환 할 수 있습니다

UIBezierPath *path = [[UIBezierPath alloc] init]; 

// Add all the lines you need 
[path addLineToPoint:p]; 

// Or points 
[path moveToPoint:p]; 

// Append paths 
[path appendPath:linePath]; 

// Make a CGPath from UIBezierPath 
CGPath myCGPath = path.CGPath; 

이 작동합니다,이 코드를 시도하지만 난 그것을 테스트하지 않은 :

- (void)makeCGPath 
{ 
    UIBezierPath *path = [[UIBezierPath alloc] init]; 

    if (lines && lines.count>0) 
    { 
     for (int i = 0; i < lines.count; i++) 
     { 
      UIBezierPath *linePath = [[UIBezierPath alloc] init]; 

      NSArray *tempArray = [lines objectAtIndex:i]; 
      CGPoint p = [[tempArray objectAtIndex:0]CGPointValue]; 
      [linePath addLineToPoint:p]; 
      for (int j = 1; j < tempArray.count; j++) 
      { 
       p = [[tempArray objectAtIndex:j]CGPointValue]; 
       [linePath addLineToPoint:p]; 
      } 
      [path appendPath:linePath]; 
     } 
    } 

    if (points && points.count > 0) 
    { 
     UIBezierPath *pointPath = [[UIBezierPath alloc] init]; 
     CGPoint p = [[points objectAtIndex:0]CGPointValue]; 
     [pointPath moveToPoint:p]; 
     for (int i = 1; i < points.count;i++) 
     { 
      p = [[points objectAtIndex:i]CGPointValue]; 
      [pointPath moveToPoint:p]; 
     } 
     [path appendPath:pointPath]; 
    } 


    drawPath = path.CGPath; 
    [self setNeedsDisplay]; 

    [lines removeAllObjects]; 
    [points removeAllObjects]; 
} 
관련 문제