xcode 在iphone的两点之间画线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8208469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
draw line between two points in iphone?
提问by Marios
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!!!
之后我把我的点放在了这里如何连接这两个点,有人能帮我吗!!!
采纳答案by Anil Kothari
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..
随意问..
回答by titaniumdecoy
You should never draw outside of drawRect:
. After detecting a touch, you need to store that information in an ivar and invoke [self setNeedsDisplay]
which will call drawRect:
at the proper time.
你永远不应该在drawRect:
. 检测到触摸后,您需要将该信息存储在 ivar 中并调用[self setNeedsDisplay]
它将drawRect:
在适当的时间调用。
Also, if you are targeting iOS 3.2+ you should consider using gesture recognizers.
此外,如果您的目标是 iOS 3.2+,您应该考虑使用手势识别器。
回答by Serhii Mamontov
Create your UIImageView subclass, add some methods which will allow to add point's on user touch (will draw them in drawRect: and also place into C-array) and one method to "commit" line. Commit line will simply go through C-array and draw them in context with CG methods.
创建您的 UIImageView 子类,添加一些允许在用户触摸时添加点的方法(将在 drawRect: 中绘制它们并放置到 C 数组中)和一种“提交”线的方法。提交线将简单地通过 C 数组并使用 CG 方法在上下文中绘制它们。