UISwipeGestureRecognizer 被 UITapGestureRecognizer 阻止

Posted

技术标签:

【中文标题】UISwipeGestureRecognizer 被 UITapGestureRecognizer 阻止【英文标题】:UISwipeGestureRecognizer blocked by UITapGestureRecognizer 【发布时间】:2013-05-21 11:49:32 【问题描述】:

我有一个需要处理平移、点击和滑动手势的视图。我有平移和点击工作,但是当我添加滑动时,它不起作用。奇怪的是,水龙头似乎以某种方式阻止了滑动,因为如果我移除水龙头,那么滑动就可以正常工作。以下是我创建手势识别器的方法。

- (void) initGestureHandlers

    UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeftGesture:)];
    swipeLeftGesture.numberOfTouchesRequired = 1;
    swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self addGestureRecognizer:swipeLeftGesture];

    UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRightGesture:)];
    swipeRightGesture.numberOfTouchesRequired = 1;
    swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self addGestureRecognizer:swipeRightGesture];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    [tapGesture setNumberOfTapsRequired:1];
    [self addGestureRecognizer:tapGesture];

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    [self addGestureRecognizer:panGesture];

很多博客建议使用 requireGestureRecognizerToFail 来处理在双击触发之前触发触发的情况,所以我尝试了,但它也没有用。

    [tapGesture requireGestureRecognizerToFail:swipeLeftGesture];
    [tapGesture requireGestureRecognizerToFail:swipeRightGesture];

如何在同一个视图中点击和滑动?

【问题讨论】:

您希望他们如何互动?它们都应该能够一直工作还是应该有一些依赖关系? 删除 numberOfTouchesRequired 和 setNumberOfTapsRequired 对单点点击手势和滑动手势来说不是必需的 我希望点击处理程序在我点击时触发,我希望滑动处理程序在我滑动时触发。现在滑动处理程序不会触发。 @Wain 我希望我的滑动处理程序首先触发,但我总是最终得到触摸手势 【参考方案1】:

评论或删除行

 //[tapGesture requireGestureRecognizerToFail:swipeLeftGesture];
 //[tapGesture requireGestureRecognizerToFail:swipeRightGesture];

还将所有客人对象委托设置为自己。像这样

- (void) initGestureHandlers

    UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeftGesture:)];
    swipeLeftGesture.numberOfTouchesRequired = 1;
    swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    swipeLeftGesture.delegate = self;
    [self addGestureRecognizer:swipeLeftGesture];

    UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRightGesture:)];
    swipeRightGesture.numberOfTouchesRequired = 1;
swipeRightGesture = self;
    swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self addGestureRecognizer:swipeRightGesture];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    [tapGesture setNumberOfTapsRequired:1];
    [self addGestureRecognizer:tapGesture];
tapGesture.delegate = self;

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    [self addGestureRecognizer:panGesture];
panGesture.delegate = self;

像这样实现委托方法

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

    return YES;

【讨论】:

就是这样!我没有设置委托。谢谢!【参考方案2】:

放这个试试看,

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

    return YES;

【讨论】:

不,我希望就这么简单。如果是这样,我不希望我的点击处理程序在我滑动时触发。 您的答案应该有效,除非我没有设置委托。哇!

以上是关于UISwipeGestureRecognizer 被 UITapGestureRecognizer 阻止的主要内容,如果未能解决你的问题,请参考以下文章

识别用户剪切/滑动手势 (UISwipeGestureRecognizer)

UISwipeGestureRecognizer 滑动长度

UISwipeGestureRecognizer 只有一个方向工作

如何检测 UISwipeGestureRecognizer 的结束?

如何使 UISwipeGestureRecognizer 在 UIScrollView 中工作

UISwipeGestureRecognizer 没有触发