2013-05-07 74 views
1

我有一个自定义UIView。手指触摸时,我会保持线条和点。然后,我做点CGPath用这种方法:多个路径不保留在屏幕上

- (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是这个样子的后续。

- (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]; 
}