UIGestureRecognizers的多个手势(iPhone,Cocos2d)

时间:2011-07-16 12:14:46

标签: iphone objective-c cocos2d-iphone

我正在使用Cocos2d渲染精灵,而UIGestureRecognizers允许用户平移,旋转和缩放精灵。

我使用以下代码孤立地工作:

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

然而,如果用户在旋转时将手指捏在一起,我想要缩放和旋转精灵(例如,照片应用会执行此操作)。不幸的是,识别器似乎陷入“旋转”或“捏合”模式,并且不会同时调用两个处理程序:(

所以,基本上,我想知道 - 这是否意味着我不能使用UIGestureRecognizers?我可以组合两个识别器并在一个处理程序中执行所有操作吗?我是否必须将UIGestureRecognizer子类化为“PinchAndRotateRecognizer”。

帮助赞赏:)

2 个答案:

答案 0 :(得分:27)

在代理中实施gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

我设置了UIPinchGestureRecognizerUIPanGestureRecognizerUIRotationGestureRecognizer,我希望它们能同时运行。我还有一个UITapGestureRecognizer想要同时被识别。我所做的就是:

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

    return NO;
}

答案 1 :(得分:-5)

只有一个手势识别器可以同时“激活”。首先触发的那个获胜。这意味着你无法将UIPinchGestureRecognizer和UIRotationGestureRecognizer结合起来以达到预期的效果。

你可以尝试按照你的说法继承UIGestureRecognizer。阅读文档中的子类注释!