在iPhone上如何在两个点之间画一条线?

3

这里是图片描述 上图显示了红点,我想将这些点连接起来。

我想在两个点之间画一条线,我的imageview中有一个图像,我想标记图像的某些部分以指示该点,使用触摸事件我放置了这些点。

   -(void) drawRect:(CGRect)rect
{

    if([pointarray count]==4)
    {
        float firstpointx= [[pointarray objectAtIndex:0]floatValue];
        float firstpointy= [[pointarray objectAtIndex:1]floatValue];
        float secondpointx= [[pointarray objectAtIndex:2]floatValue];
        float secondpointy= [[pointarray objectAtIndex:3]floatValue];

        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
        CGContextSetLineWidth(ctx, 2.0);
        CGContextMoveToPoint(ctx, firstpointx, firstpointy);///move to ur first dot
        CGContextAddLineToPoint(ctx, secondpointx, secondpointy);//add line from first dot to second dot
        CGContextSetLineCap(ctx, kCGLineCapRound);
        CGContextStrokePath(ctx);
        [pointarray removeAllObjects];//remove first two points from ur array so that next line is not drawn in continuous with previous line
    }
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ 
    pointarray=[[NSMutableArray alloc]init];
    CGPoint curPoint = [[touches anyObject] locationInView:self.view];
    [pointarray addObject:[NSNumber numberWithFloat:curPoint.x]];
    [pointarray addObject:[NSNumber numberWithFloat:curPoint.y]];
    NSLog(@"the point array is %@",pointarray);
    [self.view setNeedsDisplay]; // calls drawRectMethod
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}


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

    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        imageView.image = nil;
        return;
    }

    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.view.frame.size);
        [imageView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

    }

}

在我放置了这些点之后,我不知道如何连接这两个点,请有人帮忙!!!

你目前的结果是什么?你有任何截图吗? - Raptor
你现在能理解了吗,Shivan? - Marios
嘿!那是我的房子! - Chase Roberts
3个回答

2

请使用以下代码,它可以正常工作:

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

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        [drawImage setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"white" ofType:@"png"]]];
        return;
    }

    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 20;

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.drawImage];
    currentPoint.y -= 20;

    NSLog(@"current Point is x: %d, y: %d",currentPoint.x,currentPoint.y);

    UIGraphicsBeginImageContext(self.drawImage.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.drawImage.frame.size.width, self.drawImage.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 0.6, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

}

请随意提问...


你提出了画阿尼尔的想法,我知道如何画,我会先标记点,然后在这些点上连接线条。 - Marios

0

你不应该在drawRect:之外进行绘制。在检测到触摸后,您需要将该信息存储在ivar中,并调用[self setNeedsDisplay],这将在适当的时间调用drawRect:

此外,如果您的目标是iOS 3.2+,则应考虑使用手势识别器。


你好,titaniumdecoy,能否更详细地解释一下你的代码? - Marios
我尝试了你的方法,但输出没有成功。 - Marios

0
创建您的UIImageView子类,添加一些方法,允许在用户触摸时添加点(将在drawRect:中绘制它们并放置到C数组中),以及一个“提交”线的方法。提交线将简单地通过C数组并使用CG方法在上下文中进行绘制。

为什么要使用C数组?在这里使用NSMutableArray会更合适。 - titaniumdecoy
我正在使用NSMutableArray,只需发送一些代码以实现和检查。 - Marios
@titaniumdecoy 因为 NSMutableArray 无法存储 struct 数据类型,所以使用 CGPoint 的 C 数组会更好。 - Serhii Mamontov
嗨,Moon Light,你能给一些想法吗? - Marios
@moonlight:NSMutableArray可以存储通过NSValue对象封装的结构体值。 - titaniumdecoy
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接