UIGestureRecognizer滑动并按住

时间:2011-04-07 18:15:01

标签: iphone ios uigesturerecognizer

我正在尝试实现滚动菜单的效果。如果用户单次滑动(在任何方向上),我将为每次滑动移动一个项目。这使用UISwipeGestureReconizer工作得很好,但是如果用户在滑动后一直按住屏幕上的指针,我想继续滚动直到手指抬起。

似乎我想将UISwipeGestureRecognizerUILongPressGestureRecognizer结合起来实现这一目标。成功滑动(UISwipeGestureRecognizer)后,启用UILongPressGestureRecognizer并开始滚动,直到手指抬起(UILongPressGestureRecognizer结束)。

问题是成功滑动后不会发生UILongPressGestureRecognizer事件。

以下是一个例子:

// Create single swipe recognizer to recognize right swipes
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleSwipeFrom:)];
swipeRecognizer.delegate = self;
[_gestureView addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];

// Create long press recognizer to recognize right press and holds
longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeAndHoldFrom:)];
longPressRecognizer.delegate = self;
[longPressRecognizer setMinimumPressDuration:0.5];
[longPressRecognizer setEnabled:NO];
[_gestureView addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];

- (void)handleSingleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

    if ([recognizer state] == UIGestureRecognizerStateEnded) {
        // Move one item in direction based on recognizer.direction
        [_longPressGestureRecognizer setEnabled:YES];
    }
}

如果在滑动后我将手指放在屏幕上,则永远不会调用UILongPressGestureRecognizer事件。关于如何实现这一目标的任何想法?

1 个答案:

答案 0 :(得分:0)

尝试实现UIGestureRecognizerDelegate并使用此方法:

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

这将允许两者同时运行。您需要在手柄滑动并按住方法中添加代码,以确保仅在滑动完成后才能运行。

干杯