Multiple touch problem using UITouch/UIView in iphone

Posted by John Qualis on Stack Overflow See other posts from Stack Overflow or by John Qualis
Published on 2010-05-30T09:55:18Z Indexed on 2010/05/30 10:02 UTC
Read the original article Hit count: 542

Filed under:
|

Hi,

I am trying to implement a 2-finger "pinch" and "expand" (or enlarge) on a UIView using iPhone 3G and SDK 3.1.2. I haven't done programmed in UITouch/UIEvent before, so I appreciate any guidance to the problem I am facing.

When I touch the screen with 2 fingers I see that sometimes "touchesBegan" gives me only 1 event. I need UITouch event count == 2 to measure the distance between 2 fingers.

Hence I have to lift up my fingers and repeat this process again till I get both fingers to be detected. Is there a way around? Can I improve it? The actual resize works correctly once both fingers are detected but this issue seen at the start needs to be resolved.

I know SDK 3.2 detects gestures such as pinch but I was wondering how it can be done in 3.1.2?

The code is given below. The "OnFingerDown" function gets called randomly even when I put 2 fingers down whereas I want "onTwoFingersDown" to be called each time. Moreover the I get only 1 touch event each time I put my fingers down. I mean if "OnFingerDown" was called twice I could somehow get it to work.

Thanks in advance. Appreciate any help.

John

 - (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event

{

     UITouch* touch, *touch1;
     CGPoint location, location1;
     if([[event allTouches] count] == 1) {

               touch=[UI[[event allTouches] allObjects] objectAtIndex:0];
               location=[touch locationInView:self];
               OnFingerDown(location);
     }
     else if([[event allTouches] count] == 2) {

          touch=[[[event allTouches] allObjects] objectAtIndex:0];
          location=[touch locationInView:self];
          touch1=[[[event allTouches] allObjects] objectAtIndex:1];
          location1=[touch1 locationInView:self];

          OnTwoFingersDown(location, location1);

     }

}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk