2

I'm trying to create a drawing view by drawing the line(s) using an Array of CGPoints.

I'm currently able to draw more than one line but the problem is that I don't know how to break each line when touch is ended.

The current status is - line1 is drawn until touchended When touchbegan again, line2 is drawn as well, BUT, line1 endpoint is connected with line2 starting point.

Implementation as follows:

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

Please advise :-))

Thank you in advance,

Dudi Shani-Gabay

Dudi
  • 45
  • 1
  • 8

1 Answers1

0

In this case, I think its better for you to keep separate arrays for separate lines. Let the "pointArray" be an array having number of arrays for each line drawn.

In "touchesBegan" method, you need to add new array object to pointArray as follows:

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

In "touchesMoved", you have to replace

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

with the following:

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

In the "drawRect" method, the 'for' loop should be like this:

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);
   }
}
Sudhakar N
  • 26
  • 1