Question

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];

}

I am using CGPath and not UIBezeirPath.

So now if I want to handle one more touch, how should I proceed?

Regards Ranjit

Was it helpful?

Solution

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.

OTHER TIPS

The multipleTouchEnabled property needs to be set to YES before the view can accept multitouch.

 [self.view setMultipleTouchEnabled:YES];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top