Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

objective c - draw line between two points in iphone?

enter image description here The above picture indicate the redpoint, i like to join those points.

I like to draw a line between two points, i've image in the imageview, i like to mark some part of the image to indicate the spot,using touch event i placed the points

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

    }

}

Afer i placed the points i stuk here how to connect the two points, can someone help me!!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the following code It will work:-

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

}

Feel Free to ask..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...