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:lay开发者_JS百科er 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 :)
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;
}
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!
精彩评论