Search code examples
iphoneuitouchtouchesbegantouchesmoved

How to cancel uitouches?


In my app I want to have the users tap on the left side of the screen to increase their speed and swipe on the right side to work a joystick. However, if the user keeps holding the left side, the joystick doesn't work right.

I am using touchesBegan, touchesMoved, etc. Is there a way I could cancel the info from the first touch and have do the joystick separately?

I already tried [self touchesCancelled: touches withEvent: event]; is there any other way?


Solution

  • The Cocoa Touch framework is multi-touch aware. Therefore you don't have to cancel the first touch to work with the second touch. Just enumerate the touches and use only the one you want for the joystick in your touch delegate.

    NSArray  *allTouches = [ touches allObjects ];
    int      numTouches  = [ allTouches count ];
    for (int i=0; i<numTouches; i++) {
        UITouch *theTouch = [ allTouches objectAtIndex:i ];
        if (theTouch != leftSideHoldTouch) {
            // maybe it's a joystick touch, so handle it here
        }
    }