【问题标题】:How to stop UIPanGestureRecognizer from recognizing taps如何阻止 UIPanGestureRecognizer 识别水龙头
【发布时间】:2012-10-17 20:13:26
【问题描述】:

在我的 iPhone 应用程序的一个 UIViewControllers 上,我附加了一个 UIPanGestureRecognizer,以便当用户向左或向右滑动时,应用程序前进或返回一个屏幕。然而,问题是当用户在屏幕上点击(而不是滑动)时,它仍然会前进。我怎样才能阻止这种情况发生。我在下面粘贴了相关代码:

-(void) addGestureRecognizer
{
    UIPanGestureRecognizer *pan;
    pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)];
    [pan setMinimumNumberOfTouches:1];
    [self.view addGestureRecognizer:pan];
}

-(void) swipeRecognized: (UIPanGestureRecognizer  *) recognizer
{
    if(recognizer.state != UIGestureRecognizerStateBegan) return;
    CGPoint velocity = [recognizer velocityInView:self.view];
    if(velocity.x > 0)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        @try
        {
            [self performSegueWithIdentifier:NEXT_STEP_SEGUE sender:self];
        }
        @catch (NSException *exception)
        {
            //Silently die...muhaha
        }
    }
}

【问题讨论】:

    标签: objective-c ios uigesturerecognizer segue


    【解决方案1】:

    我建议使用UISwipeGestureRecognizer 进行滑动。您使用平底锅有什么特殊原因吗?

    使用UISwipeGestureRecognizer,您可以指定识别手势的方向。

    使用适当的手势对您的用户也更好。这样,他们就会有宾至如归的感觉:)

    【讨论】:

    • 我使用它是为了检测 1 或 2 次手指滑动以及任一方向的滑动
    • UISwipeGestureRecognizers 还有一个名为numberOfTouchesRequired 的属性。您可以轻松添加两个识别器,每个方向一个。我真的会敦促你使用适当的手势。在您的解决方案中,手指移动 1 毫米会触发平移,但这几乎不是滑动...
    • UISwipeGestureRecognizer 和 UIPanGestureRecognizer 之间到底有什么区别?
    • 一个用于滑动...单向快速滑动,一个用于平移...可以改变方向的连续拖动动作。
    【解决方案2】:

    您应该按照 fguchelaar 的建议使用 UISwipeGestureRecognizer,或者使用 translationInView: 方法来确定用户实际移动手指的距离(对于轻敲,应该接近于零)。

    如果状态不是UIGestureRecognizerStateBegan(您方法中的第一行),您也不应该提前返回,否则您的方法只会在手势开始时被调用一次,而不是在手势期间。如果这就是您真正想要的,那么您只需要UISwipeGestureRecognizer。平移手势识别器的好处主要在于您可以跟踪用户的手指并提供直接反馈(例如在手指仍然向下时移动视图)。

    【讨论】:

      猜你喜欢
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      相关资源
      最近更新 更多