Get all current UITouches, including ones that aren't being updated

346 views Asked by At

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!

1

There are 1 answers

2
Akhilrajtr On

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