【问题标题】:UIGestureRecognizer swipe and holdUIGestureRecognizer 滑动并按住
【发布时间】:2011-04-07 18:15:01
【问题描述】:

我正在尝试实现滚动菜单的效果。如果用户进行单次滑动(在任何方向),我将为每次滑动移动一个项目。使用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 事件。关于如何实现这一点的任何想法?

【问题讨论】:

标签: iphone ios uigesturerecognizer


【解决方案1】:

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

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

这将允许两者同时运行。您需要将代码添加到 handle swipe and hold 方法中,以确保它仅在滑动完成后运行。

干杯

【讨论】:

  • 我已经这样做了。我为此设置了 UISwipeGestureRecognizer 和 UILongPressGestureRecognizer(delegate = self),并且为每个识别器成功调用它并返回 YES。我也不会在滑动结束后启用长按(它也有延迟)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-17
  • 2022-09-23
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多