2

So I'm doing custom gesture recognizing and I've ran into trouble. I need to monitor all the current touches, but touchesBegan, touchesEnded, touchesMoved and touchesCancelled all only give me the touches that they are monitoring. I need to do calculations on all the touches, regardless of wether they are being monitored or not. I've done some research and I've seen suggestions to monitor the touches myself, but it hasn't been going well for me. Here is my code:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */
    [myTouches unionSet:touches];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    NSArray *objects = [myTouches allObjects];

    for (UITouch *touch in touches) {
        for (UITouch *touch2 in objects) {
            if (CGPointEqualToPoint([touch2 locationInView:self.view], [touch previousLocationInView:self.view])) {
                [myTouches removeObject:touch2];
                [myTouches addObject:touch];
            }
        }
    }
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [myTouches minusSet:touches];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    [myTouches minusSet:touches];

    NSArray *objects = [myTouches allObjects];

    for (UITouch *touch in touches) {
        for (UITouch *touch2 in objects) {
            if (CGPointEqualToPoint([touch2 locationInView:self.view], [touch previousLocationInView:self.view])) {
                [myTouches removeObject:touch2];
            }
        }
    }
}

What I want: myTouches should be an accurate representation of all the touches on the screen. What I get: myTouches constantly grows, although sometimes touches from events are correctly registered as the same as touches in myTouches. I also did a test where I had an integer count the number of touches registered in touchesBegan and subtracted the touches registered in touchesEnded and the number always came out to be positive, meaning that more touches are being registered than ended. Please help!

twlkyao
  • 14,302
  • 7
  • 27
  • 44
WolfLink
  • 3,308
  • 2
  • 26
  • 44
  • you are taking "touchesCancelled" also in consideration? – spider1983 Dec 31 '13 at 09:53
  • I do have `[myTouches minusSet:touches]` in `touchesCancelled:`. – WolfLink Dec 31 '13 at 17:32
  • 1
    The UIEvent has a different array of touches than the NSSet `touches`. I had to use this array to do manual multitouch events. I'm not sure if it is the complete set, but it is more complete than `touches`. Use `[event allTouches]` to get that array. – Putz1103 Dec 31 '13 at 18:07
  • Thanks it turns out that was all I needed to do. `allTouches` seems to give me all the touches on the screen, which is what I need. Make it an answer and I'll accept it. – WolfLink Jan 03 '14 at 19:51

1 Answers1

1

Try subclass of UIApplication to monitor all the touches in application.

    @interface MyUIApplication : UIApplication

then use the event sendEvent in MyUIApplication

    - (void)sendEvent:(UIEvent *)event {

        [super sendEvent:event];
        NSSet *allTouches = [event allTouches];
        if ([allTouches count] > 0) {

             //To get phase of a touch
             UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
             if (phase == UITouchPhaseBegan){

                 //Do the stuff
             }
             //also you can use UITouchPhaseMoved, UITouchPhaseEnded,
             // UITouchPhaseCancelled and UITouchPhaseStationary
        }
    }

and don't forget to add the class in main

    int main(int argc, char *argv[])
    {
          @autoreleasepool {
                return UIApplicationMain(argc, argv, NSStringFromClass([MyUIApplication class]), NSStringFromClass([AppDelegate class]));
          }
    }
Akhilrajtr
  • 5,170
  • 3
  • 19
  • 30