Search code examples
iosxcodetouchtouchesbegan

IOS: correct number of touch on screen


In my app I need to catch the exact number of finger on the screen, I try two ways but I have 2 different problem.

First way:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSArray *allTouches = [touches allObjects];
    int count = [allTouches count];
    NSLog(@"number of touch:%d", count);
}

this give me a NOT accurate number of touches if I use more finger at the same time.

Second way:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    int num = [touches count];
    totalTouch = totalTouch+num;
    NSLog(@"number of touch:%d", totalTouch);
}

in this way I use a global var (totalTouch) that I increment everytime touchbegan is called and I have a perfect number of touches. Naturally i set at '0' this var in the touchend

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    totalTouch = 0;
}

My problem is that, with the second way I do a control in touchbegan, this:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    int num = [touches count];
    totalTouch = totalTouch+num;

    if (totalTouch == numberToVerify){
       //IT ENTER HERE EVERYTIME INSIDE THIS IF
    }
    else{
    }
 }

so everytime it enter inside if-conditions, and I don't want it, I want do this control only when I have a final number of touch...


Solution

  • Within your

     - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
         int num = [touches count];
         totalTouch = totalTouch+num;
         NSLog(@"number of touch:%d", totalTouch);
      } 
    

    You can get the number of fingers on the screen with

      [[event allTouches]count]
    

    This can be accessed from - (void)touchesBegan: - (void)touchesEnded: or - (void)touchesMoved: