如何在iphone中使用UItouch或橡皮筋样式的绘图通过触摸绘制直线
我是iPhone开发新手 我在做一个应用程序,我试图画一条直线,这样它就不会有任何不规则的曲线,它必须是平滑的 我使用了核心图形,但它似乎不起作用 我只想用UITouch将这些线画成红色,使其笔直 我应该使用openGl吗 如果是,我如何实施如何在iphone中使用UItouch或橡皮筋样式的绘图通过触摸绘制直线,iphone,ios4,core-graphics,line,uitouch,Iphone,Ios4,Core Graphics,Line,Uitouch,我是iPhone开发新手 我在做一个应用程序,我试图画一条直线,这样它就不会有任何不规则的曲线,它必须是平滑的 我使用了核心图形,但它似乎不起作用 我只想用UITouch将这些线画成红色,使其笔直 我应该使用openGl吗 如果是,我如何实施 drawImage = [[UIImageView alloc] initWithImage:nil]; drawImage.frame = viewField.frame; [self.view addSubview:drawImage]; - (
drawImage = [[UIImageView alloc] initWithImage:nil];
drawImage.frame = viewField.frame;
[self.view addSubview:drawImage];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
return;
}
lastPoint = [touch locationInView:viewField];
//lastPoint.y = 20;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:viewField];
//currentPoint.y -= 20; // only for 'kCGLineCapRound'
UIGraphicsBeginImageContext(viewField.frame.size);
//Albert Renshaw - Apps4Life
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); // for size
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); //values for R, G, B, and Alpha
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch tapCount] == 2) {
drawImage.image = nil;
return;
}
if(!mouseSwiped) {
//if color == green
UIGraphicsBeginImageContext(viewField.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, 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());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
提前谢谢 从你发布的图片判断,我建议你改变方法。 双击开始绘图。 然后在多边形中的每个边点上点击=ToucheStarted和touchesEnded,而不是touchesMoved。 沿着这些点画一条直线。 双击结束多边形。
如果终点离起点足够近,您可以决定自动关闭路径。根据您发布的图像判断,我建议您改变方法。 双击开始绘图。 然后在多边形中的每个边点上点击=ToucheStarted和touchesEnded,而不是touchesMoved。 沿着这些点画一条直线。 双击结束多边形。
如果终点与起点足够接近,您可以决定自动关闭路径。好:我是初学者,所以我将尝试我技能范围内的事情,并在做这些事情时提高,以便以后我可以做更难的事情。坏:我是一个初学者,所以我会努力尝试一些事情,而不是自己去做,我会问周围的代码来做,这样我就不会学到任何东西。你写的代码到目前为止是什么样子的?你能把它贴在这里吗?你在哪一部分有问题?如果你想画一条直线,不要存储每一个接触过的点。存储起始点和用户当前触摸的位置,并从一个点到另一个点画一条线。看起来你在某个地方复制了一个手绘代码示例fon,而这不是你想要的。而且,现在的问题是你昨天应该发布什么-它有你吃过的代码,以及对问题的清晰描述-做得好:谢谢@jrturton,但是你能澄清你在说什么,并显示代码中的错误吗?通过这些错误,我可以得到想要的结果,或者我应该完全改变它。好:我是一个初学者,所以我将尝试我技能范围内的事情,并在做这些事情的同时提高,这样以后我可以做更难的事情。坏:我是一个初学者,所以我会努力尝试一些事情,而不是自己去做,我会问周围的代码来做,这样我就不会学到任何东西。你写的代码到目前为止是什么样子的?你能把它贴在这里吗?你在哪一部分有问题?如果你想画一条直线,不要存储每一个接触过的点。存储起始点和用户当前触摸的位置,并从一个点到另一个点画一条线。看起来你在某个地方复制了一个手绘代码示例fon,而这不是你想要的。而且,现在的问题是你昨天应该发布什么-它有你吃过的代码,以及对问题的清晰描述-做得好:谢谢@jrturton,但是你能澄清一下你在说什么,并在代码中显示错误,通过它我可以得到想要的结果,或者我应该完全改变它吗。