问题描述
我正在处理绘图项目,并且运行得很好,唯一的问题是,它仅适用于单点触控。我想进行多点触控绘图,因此如果用户用两只手指进行绘图,则应该绘制它们
I am working with drawing project, and it is quite working well, the only problem is that, it works only with single touch.I want to do multitouch drawing, so that if user draws with his two fingers, then it should draw them
下面是我的单点触摸绘制代码
Below is my code for single touch drawing
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//LocationInView returns the current location of the reciever in coordinate system of the given View.
m_previousPoint1 = [touch locationInView:self];
m_previousPoint2 = [touch locationInView:self];
m_currentPoint = [touch locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//AnyObject:- Returns one of the objects in the set, or nil if the set contains no objects.
UITouch *touch = [touches anyObject];
m_previousPoint2 = m_previousPoint1;
m_previousPoint1 = m_currentPoint;
m_currentPoint = [touch locationInView:self];
if(m_drawStep != ERASE)
{
m_drawStep = DRAW;
m_drawing = TRUE;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);//creates a graphics context suitable for use as an image(size of the image,opquae,scale, if scale = 0.0, means platform will take care of scaling)
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
m_curImage = UIGraphicsGetImageFromCurrentImageContext();// to turn the context into a UIImage
UIGraphicsEndImageContext();
NSDictionary *lineInfo = [NSDictionary dictionaryWithObjectsAndKeys:m_curImage, @"IMAGE",
nil];
}
我使用的是CGPath,而不是UIBezeirPath。
I am using CGPath and not UIBezeirPath.
现在,如果我想再处理一次,我应该如何进行?
So now if I want to handle one more touch, how should I proceed?
要注意
兰吉特
RegardsRanjit
推荐答案
很好的touchesMoved方法,将为您提供触摸(NSSet)。
现在在您的代码中您仅使用 [touches anyObject]
,它将返回一个UITouch。
使用该集合中的所有触摸。
well in touchesMoved method, you will be supplied with touches (NSSet).
Right now in your code you are using just [touches anyObject]
, which will return one UITouch.use all touches in that set.
NSArray *touchesArray = [touches allObjects];
并遍历所有对象
只需将下面的代码替换为旧代码即可,如果弹出任何新错误,请进行回复。
Just replace the old code with below code.Reply if any new errors popup.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// NSLog(@"began called touches:%d events:%d",touches.count,event.allTouches.count);
UITouch *touch = [touches allObjects][0];
UITouch *touch2;
if(touches.count==2)
touch2 = [touches allObjects][1];
if(event.allTouches.count==1){
lastPoint1 = [touch locationInView:self.view];
return;
}
else if (event.allTouches.count==2){
if(touches.count==2){
lastPoint1 = [touch locationInView:self.view];
lastPoint2 = [touch2 locationInView:self.view];
return;
}
else{
lastPoint2 = [touch locationInView:self.view];
return;
}
}
}
-(BOOL)pointCloseToLastPoint1:(CGPoint)p{
if(((p.x - lastPoint1.x)*(p.x - lastPoint1.x) + (p.y - lastPoint1.y)*(p.y - lastPoint1.y)) < ((p.x - lastPoint2.x)*(p.x - lastPoint2.x) + (p.y - lastPoint2.y)*(p.y - lastPoint2.y)))
return YES;
else
return NO;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// NSLog(@"moved called %d %d",touches.count,event.allTouches.count);
UITouch *touch = [touches allObjects][0];
UITouch *touch2;
CGPoint cp1,cp2,temp;
if(touches.count==2)
touch2 = [touches allObjects][1];
if(event.allTouches.count ==1){
cp1 = [touch locationInView:self.view];
}
if(event.allTouches.count==2){
if(touches.count==1){
cp1 = [touch locationInView:self.view];
if(![self pointCloseToLastPoint1:cp1]){
temp =lastPoint2;
lastPoint2 =lastPoint1;
lastPoint1=temp;
}
}
else{
cp1 =[touch locationInView:self.view];
cp2 =[touch2 locationInView:self.view];
if(![self pointCloseToLastPoint1:cp1]){
temp =lastPoint2;
lastPoint2 =lastPoint1;
lastPoint1=temp;
}
}
}
// NSLog(@"loc moved: %@",NSStringFromCGPoint([touch locationInView:self.view]));
UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint1.x, lastPoint1.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), cp1.x, cp1.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempDrawImage setAlpha:opacity];
UIGraphicsEndImageContext();
lastPoint1 = cp1;
if(touches.count ==2){
UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint2.x, lastPoint2.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), cp2.x, cp2.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempDrawImage setAlpha:opacity];
UIGraphicsEndImageContext();
lastPoint2 = cp2;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// NSLog(@"touches ended %d %d",touches.count,event.allTouches.count);
UITouch *touch = (UITouch *)[touches allObjects][0];
if(event.allTouches.count==2 && touches.count==1){
CGPoint cp = [touch locationInView:self.view];
if([self pointCloseToLastPoint1:cp]){
lastPoint1=lastPoint2;
}
}
}
希望如此帮助。
这篇关于iOS中的Mutlitouch绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!