1

我有一个习惯UIView。当手指触摸移动时,我会保持线条和点。然后我CGPath用这个方法指出:</p>

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

看起来很奇怪,它只显示屏幕上的最后一行。它出什么问题了?

如何将所有这些路径保留在屏幕上?

4

1 回答 1

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];
}
于 2013-05-17T01:15:47.943 回答