【问题标题】:Drawing more than one line/path on iPhone using Array of points使用点数组在 iPhone 上绘制多条线/路径
【发布时间】:2011-10-22 20:30:27
【问题描述】:

我正在尝试通过使用 CGPoints 数组绘制线条来创建绘图视图。

我目前可以画多条线,但问题是我不知道如何在触摸结束时断开每一行。

目前的状态是—— 绘制 line1 直到 touchended 再次开始触摸时,line2也被绘制,但是line1端点与line2起点相连。

实现如下:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSUInteger tapCount = [[touches anyObject] tapCount];
    if (tapCount == 2)
    {
        [pointArray removeAllObjects];
        [self setNeedsDisplay];
    }
    else
    {
        if ([pointArray count] == 0)
            pointArray = [[NSMutableArray alloc] init];
        UITouch *touch = [touches anyObject];
        start_location = [touch locationInView:self];
        [pointArray addObject:[NSValue valueWithCGPoint:start_location]];
    }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    current_location = [touch locationInView:self];
    [pointArray addObject:[NSValue valueWithCGPoint:current_location]];
    [self setNeedsDisplay];    
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

}



-(void)drawRect:(CGRect)rect
{
    if ([pointArray count] > 0)
    {
        int i;
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, 2.0);
        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
        for (i=0; i < ([pointArray count] -1); i++)
        {
            CGPoint p1 = [[pointArray objectAtIndex:i]CGPointValue];
            CGPoint p2 = [[pointArray objectAtIndex:i+1]CGPointValue];
            CGContextMoveToPoint(context, p1.x, p1.y);
            CGContextAddLineToPoint(context, p2.x, p2.y);
            CGContextStrokePath(context);
        }
    }
}

请指教:-))

提前谢谢你,

杜迪·沙尼-加贝

【问题讨论】:

    标签: ios4 drawrect cgcontext


    【解决方案1】:

    在这种情况下,我认为最好为单独的行保留单独的数组。让“pointArray”是一个数组,每条线都有多个数组。

    在“touchesBegan”方法中,需要将新的数组对象添加到pointArray,如下:

    start_location = [touch locationInView:self];
    NSMutableArray *newLineArray = [[NSMutableArray alloc] init];
    [pointArray addObject:newLineArray];
    [[pointArray lastObject] addObject:[NSValue valueWithCGPoint:start_location]];
    

    在“touchesMoved”中,你必须替换

    [pointArray addObject:[NSValue valueWithCGPoint:current_location]];
    

    以下内容:

    [[pointArray lastObject] addObject:[NSValue valueWithCGPoint:current_location]];
    

    在“drawRect”方法中,'for'循环应该是这样的:

    for (i=0; i < [pointArray count]; i++)
    {
       for (int j=0; j < ([[pointArray objectAtIndex:i] count] -1); j++)
       {
            CGPoint p1 = [[[pointArray objectAtIndex:i] objectAtIndex:j]CGPointValue];
            CGPoint p2 = [[[pointArray objectAtIndex:i] objectAtIndex:j+1]CGPointValue];
            CGContextMoveToPoint(context, p1.x, p1.y);
            CGContextAddLineToPoint(context, p2.x, p2.y);
            CGContextStrokePath(context);
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 2018-12-17
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      相关资源
      最近更新 更多