iOS中的Mutlitouch绘图

我正在和绘图工程一起工作,而且工作得很好,唯一的问题是,它只能用单点触摸工作。我想要做多点触摸绘图,所以如果用户用两个手指画,那么就应该绘制它们

下面是我的单触式绘图的代码

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

所以现在如果我想再处理一次,我该怎么处理呢?

关心Ranjit

以及touchesMoved方法,你将被提供触摸(NSSet)。
现在在你的代码中,你正在使用[touches anyObject] ,这将返回一个UITouch。 使用该集合中的所有接触。

 NSArray *touchesArray = [touches allObjects]; 

并遍历所有这些

只需用下面的代码replace旧的代码。如果有任何新的错误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(((px - lastPoint1.x)*(px - lastPoint1.x) + (py - lastPoint1.y)*(py - lastPoint1.y)) < ((px - lastPoint2.x)*(px - lastPoint2.x) + (py - lastPoint2.y)*(py - 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; } } } 

希望这可以帮助。

在视图可以接受多点触控之前,multipleTouchEnabled属性需要设置为YES。

  [self.view setMultipleTouchEnabled:YES];