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

发布于 2024-12-11 12:28:45 字数 1774 浏览 0 评论 0原文

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

我目前可以绘制多条线,但问题是我不知道触摸结束时如何打破每条线。

目前的状态是—— line1 被绘制直到被触摸 当再次 touchbegan 时,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);
        }
    }
}

请告知:-))

预先感谢您,

Dudi Shani-Gabay

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2024-12-18 12:28:45

在这种情况下,我认为最好为单独的行保留单独的数组。令“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);
   }
}

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);
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文