Search code examples
objective-ccocos2d-iphonebox2duigesturerecognizeruipinchgesturerecognizer

I need help subclassing UIPinchGestureRecognizer to find the location of the touches during the pinch


I need to track where the touches are while the user pinches to do some fancy bits and pieces, for a game, unrelated to zooming. I can detect pinches but I can't find the location of the taps during the pinch, only the midpoint (even using the private variable touches doesn't work).

An ideal outcome would be (where Box2DPinchRecognizer is the name of my UIPinchRecognizer subclass):

Box2DPinchRecognizer *pinchRecognizer = [[Box2DPinchRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:) withObject:(NSArray*)touches];

Where touches is an array of the two CGPoints indicating where your fingers are.


Solution

  • No need to subclass anything. You should be able to ask any UIGestureRecognizer for the location of its current touches in its action method:

    - (void)pinchGesture:(UIGestureRecognizer *)recognizer
    {
        NSUInteger numberOfTouches = recognizer.numberOfTouches;
        NSMutableArray *touchLocations = [NSMutableArray arrayWithCapacity:numberOfTouches];
        for (NSInteger touchIndex = 0; touchIndex < numberOfTouches; touchIndex++) {
            CGPoint location = [recognizer locationOfTouch:touchIndex inView:self.view];
            [touchLocations addObject:[NSValue valueWithCGPoint:location]];
        }
        ...
    }
    


    Edit:
    At the end of code square bracket is added.