gpt4 book ai didi

iphone - 在 iphone 中的两点之间画线?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:24:51 61 4
gpt4 key购买 nike

enter image description here上图是红点,我喜欢加入这些点。

我喜欢在两点之间画一条线,我在 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();

}

}

在我把点放在这里之后,我想知道如何连接这两点,有人能帮我吗!!!

最佳答案

使用下面的代码它会起作用:-

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

}

欢迎提问..

关于iphone - 在 iphone 中的两点之间画线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8208469/

61 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com