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];
and iterate over all of them
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;
}
}
}
Hope this helps.