在设置requireGestureToFail后,将UIPanGestureRecognizer和UISwipeGestureRecognizer添加到同一视图会导致冲突 [英] Adding a UIPanGestureRecognizer and a UISwipeGestureRecognizer to same view causes conflicts after setting requireGestureToFail

查看:923
本文介绍了在设置requireGestureToFail后,将UIPanGestureRecognizer和UISwipeGestureRecognizer添加到同一视图会导致冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一个视图中添加了一个滑动手势识别器和一个平移手势识别器。这些手势应该是彼此独有的。

I added a swipe gesture recognizer and a pan gesture recognizer to the same view. These gestures should be exclusive to each other.

为了做到这一点,我在滑动手势上添加了约束

In order to do this I added the constraint on the swipe gesture

[swipeGesture requireGestureToFail:panGesture];

(因为平移手势应该优先)

(because the pan gesture should get precedence)

问题在于,即使在非常快速的滑动过程中也会始终调用平移手势。

Problem is that the pan gesture is always invoked - even during a very fast swipe.

为了克服这个问题,我将自己设置为平移手势的委托。在委托方法中,我设置了如下代码:

In order to over come this I set myself as the pan gesture's delegate. In the delegate method I set up some code as follows:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    // check if it is the relevant view
    if (gestureRecognizer.view == self.myViewWithTwoGestures)
    {
        // check that it is the pan gesture
        if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
        {
            UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
            CGPoint velocity = [pan velocityInView:gestureRecognizer.view];
            // added an arbitrary velocity for failure
            if (ABS(velocity.y) > 100)
            {
                // fail if the swipe was fast enough - this should allow the swipe gesture to be invoked
                return NO;
            }
        }
    }
    return YES;
}

是否有建议的速度来确保良好的行为?有没有其他方法可以强制平移手势失败?

Is there a suggested velocity to ensure good behavior? Is there another way to force the pan gesture to fail?

推荐答案

根据Apple的文档这里(在声明两个手势识别器的特定订单)同时获取 UIPanGestureRecognizer UISwipeGestureRecognizer 处理同一视图的方法是要求 UISwipeGesureRecognizer 在调用 UIPanGestureRecognizer 之前失败(与你所写的相反)。这可能与滑动手势也是平移手势这一事实有关,但相反的情况不一定如此(请参阅此 SO 问题)。

According to Apple's documentation here (under Declaring a Specific Order for Two Gesture Recognizers) the way to get both UIPanGestureRecognizer and UISwipeGestureRecognizer to work on the same view is by requiring the UISwipeGesureRecognizer to fail before calling the UIPanGestureRecognizer (the opposite of what you wrote). This probably has something to do with the fact the a swipe gesture is also a pan gesture but the opposite is not necessarily true (see this SO question).

我写了这段小代码,它设法识别平移和滑动手势:

I wrote this little piece of code and it manages to recognize both pan and swipe gestures:

UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panned:)];
UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped:)];
[pan requireGestureRecognizerToFail:swipe];
swipe.direction = (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight);

-(void)panned:(UIPanGestureRecognizer *)gesture
{
    NSLog(@"Pan");
}

-(void)swiped:(UISwipeGestureRecognizer *)gesture
{
    NSLog(@"Swipe");
}

这不是你希望的那样好(因为你需要)滑动手势失败,在手势开始之前有一个小延迟)但它确实有效。
您发布的代码可让您根据自己的喜好微调手势。

This doesn't work as well as you'd hope (since you need the swipe gesture to fail there's a small delay before the pan gesture starts) but it does work. The code you posted however gives you the ability to fine tune the gestures to your liking.

这篇关于在设置requireGestureToFail后,将UIPanGestureRecognizer和UISwipeGestureRecognizer添加到同一视图会导致冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆