nikz
nikz

Reputation: 498

Multiple Gestures for UIGestureRecognizers (iPhone, Cocos2d)

I'm using Cocos2d to render a sprite, and UIGestureRecognizers to allow the user to Pan, Rotate and Scale the sprite.

I've got each working in isolation using code like the following:

UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:layer action:@selector(handlePinchFrom:)] autorelease];
[viewController.view addGestureRecognizer:pinchRecognizer];

UIRotationGestureRecognizer *rotationRecognizer = [[[UIRotationGestureRecognizer alloc] initWithTarget:layer action:@selector(handleRotationFrom:)] autorelease];
[viewController.view addGestureRecognizer:rotationRecognizer];

However, I want to both scale and rotate the sprite if the user pinches their fingers together whilst rotating (the Photos app does this, for instance). Unfortunately though, the recognizer seems to get stuck in either "rotate" or "pinch" mode, and won't call both handlers at the same time :(

So, basically, I want to know - does this mean I can't use UIGestureRecognizers? Can I combine two recognizers and do all of the actions in a single handler? Will I have to subclass UIGestureRecognizer to be something like "PinchAndRotateRecognizer".

Help appreciated :)

Upvotes: 6

Views: 6827

Answers (2)

Johannes Fahrenkrug
Johannes Fahrenkrug

Reputation: 44836

Just implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: in your delegate.

I have a UIPinchGestureRecognizer, a UIPanGestureRecognizer and a UIRotationGestureRecognizer set up and I want them all to work at the same time. I also have a UITapGestureRecognizer which I do not want to be recognized simultaneously. All I did was this:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if (![gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && ![otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        return YES;
    }

    return NO;
}

Upvotes: 27

Felix
Felix

Reputation: 35394

Only one gesture recognizer can be "active" at the same time. The one which is triggered first wins. That means you can't combine UIPinchGestureRecognizer and UIRotationGestureRecognizer to achieve the desired effect.

You could try to subclass UIGestureRecognizer as you said. Read the subclassing notes in the documentation!

Upvotes: -5

Related Questions