0

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

Ranjit
  • 4,576
  • 11
  • 62
  • 121

2 Answers2

1

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.

santhu
  • 4,796
  • 1
  • 21
  • 29
  • Hi @santhu, thanks for your reply, I used it , but the problem is that I am not getting two separate lines, the problem is that it drawing a line by considering two touch points, so if I draw using my two fingers, I get only horizontal lines. So if you explain how to iterate, it will be very helpful to me. – Ranjit Dec 22 '13 at 08:56
  • In touches moved, if touches count is equal to 2, print(NSLOG) locationInView of each UITouch object. you can easily see that traces the touch moving points. It worked for me. – santhu Dec 22 '13 at 09:14
  • hello @santhu, thanks for your code, I tried it, firstly it works for only 2 fingers and not more , secondly, I have created a sample project for simple drawing using your code, here is the link https://www.dropbox.com/s/jpy7o0xg6oe0c01/MultiTouch.zip ..The problem which I am facing here is that sometimes whenever I touch with two fingers , it take those points and draw a line, but that is not correct behaviour right.So I request you to go through my project and suggest what I am doing wrong. – Ranjit Dec 23 '13 at 07:16
  • Hello @santhu, did you go through it. – Ranjit Dec 23 '13 at 08:30
  • Hello @santhu please go through this http://stackoverflow.com/questions/20761686/track-touch-points-in-mutlitouch – Ranjit Dec 24 '13 at 13:39
  • Hi @santhu, what if touch count is greater than 2, I mean user touches with more fingers – Ranjit Dec 25 '13 at 04:05
  • Hi @santhu,This works fine now, but only for two touches, actually in my question I said two fingers, because of that , you have given me this answer, but how to track any number of touches? – Ranjit Dec 25 '13 at 04:15
  • Hello @santhu, did you look at my previous comment. – Ranjit Dec 25 '13 at 12:53
  • Hello @santhu, are you there, please reply – Ranjit Dec 30 '13 at 06:51
0

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

 [self.view setMultipleTouchEnabled:YES];
Mavericks
  • 524
  • 3
  • 5