Ios4 使用点数组在iPhone上绘制多条线/路径

Ios4 使用点数组在iPhone上绘制多条线/路径,ios4,drawrect,cgcontext,Ios4,Drawrect,Cgcontext,我试图通过使用CGPoints数组绘制线来创建工程视图 我现在可以画多条线,但问题是当触摸结束时,我不知道如何断开每条线 目前的状况是- 线1一直绘制到触摸结束 再次开始触摸时,也会绘制line2,但line1端点与line2起点相连 执行情况如下: -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSUInteger tapCount = [[touches anyObject] tapCount];

我试图通过使用CGPoints数组绘制线来创建工程视图

我现在可以画多条线,但问题是当触摸结束时,我不知道如何断开每条线

目前的状况是- 线1一直绘制到触摸结束 再次开始触摸时,也会绘制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);
        }
    }
}
start_location = [touch locationInView:self];
NSMutableArray *newLineArray = [[NSMutableArray alloc] init];
[pointArray addObject:newLineArray];
[[pointArray lastObject] addObject:[NSValue valueWithCGPoint:start_location]];
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event
{
NSU整数tapCount=[[Touchs anyObject]tapCount];
如果(tapCount==2)
{
[pointArray removeAllObjects];
[自我设置需要显示];
}
其他的
{
如果([pointArray count]==0)
pointArray=[[NSMutableArray alloc]init];
UITouch*touch=[触摸任何对象];
开始位置=[触摸位置查看:自];
[pointArray addObject:[NSValue valueWithCGPoint:start_location];
}
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*touch=[触摸任何对象];
当前位置=[触摸位置查看:自];
[pointArray addObject:[NSValue valueWithCGPoint:当前_位置];
[自我设置需要显示];
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event
{
}
-(void)drawRect:(CGRect)rect
{
如果([pointArray count]>0)
{
int i;
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(上下文,2.0);
CGContextSetStrokeColorWithColor(上下文[UIColor blueColor].CGColor);
对于(i=0;i<([pointArray count]-1);i++)
{
CGPoint p1=[[pointArray objectAtIndex:i]CGPointValue];
CGPoint p2=[[pointArray objectAtIndex:i+1]CGPointValue];
CGContextMoveToPoint(上下文,p1.x,p1.y);
CGContextAddLineToPoint(上下文,p2.x,p2.y);
CGContextStrokePath(上下文);
}
}
}
请告知:-)

提前谢谢大家,


Dudi Shani Gabay

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

在“touchesBegind”方法中,您需要按如下方式向pointArray添加新的数组对象:

-(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);
        }
    }
}
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);
   }
}
for(i=0;i<[pointArray count];i++)
{
对于(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(上下文,p1.x,p1.y);
CGContextAddLineToPoint(上下文,p2.x,p2.y);
CGContextStrokePath(上下文);
}
}

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

在“touchesBegind”方法中,您需要按如下方式向pointArray添加新的数组对象:

-(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);
        }
    }
}
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);
   }
}
for(i=0;i<[pointArray count];i++)
{
对于(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(上下文,p1.x,p1.y);
CGContextAddLineToPoint(上下文,p2.x,p2.y);
CGContextStrokePath(上下文);
}
}