如何在iPad上同时允许多个长按手势?

时间:2011-12-22 21:20:05

标签: ios ipad gesture multi-touch

我目前有一些可拖放的图层链接到UILabels。我有一个LongPressRecognizer在标签上拾取长按,并在拖动标签的情况下移动图层。

我通过在识别器目标中捕捉手势来实现这一点:

- (void)handleDragging:(UILongPressGestureRecognizer *)gestureRecognizer
{
    switch ([gestureRecognizer state]) 
    {
        case UIGestureRecognizerStateBegan:
            [self startDragging:gestureRecognizer];
            break;
        case UIGestureRecognizerStateChanged:
            [self doDrag:gestureRecognizer];
            break;
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
            [self stopDragging:gestureRecognizer];
            break;
        default:
            break;
    }
}

但是,我想让用户在拖动一个标签时能够在另一个标签上执行额外的长按以同时拖动多个标签。由于识别器当前接受1次触摸(numberOfTouchesRequired = 1),因此它似乎无法接收其他事件。但是,每次在iPad上发生另一个触摸事件时,都会触发gestureRecognizerStateChanged事件。但是,识别器上的numberOfTouches仍显示单次触摸。

给每个标签自己的LongPressRecognizer是最好的解决方案,还是我可以用一个识别器做更优雅的事情?

1 个答案:

答案 0 :(得分:6)

在您的视图控制器中......

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {        
    return YES;
}

不要忘记

recognizer.delegate = self;

创建每个手势时......