如何阻止从superview到子视图的手势? [英] How to block a gesture from superview to subview?

查看:129
本文介绍了如何阻止从superview到子视图的手势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个模块,每当我在视图上滑动时,将添加两个具有视图的一半大小的子视图。这些子视图有自己的手势(例如:pan,...)。我第一次滑动,没关系,因为没有创建子视图。但是一旦创建了子视图,每次我滑动时,滑动手势都会传递到其子视图。 :(,所以我必须滑动2次才能分开。

I'm writing a module that everytime I swipe on a view, two sub views with a half size of the view will be added. Those subviews have their own gestures (eg: pan,...). The first time I swipe, it's OK because none of subview has been created. But once the subview been created, everytime I swipe, the swipe gesture is alway pass to its subviews. :(, so I have to swipe 2 times to divide.

我想知道有没有办法阻止滑动传递给它的子视图?谢谢。

I want to know is there any way to block swipe passing to its subview? Thank you.

UPDATE

我使用了shouldRecognizeSimultaneouslyWithGestureRecognizer使这些手势同时工作。但是仍然有一些问题。父视图有它的滑动手势,子视图有Pan手势。由于我使用souldRecognizeSimultaneouslyWithGestureRecognizer,有时当我平移时,滑动手势会触发。所以,你知道如何在Pan处于活动状态时禁用Swipe吗?

UPDATE
I used shouldRecognizeSimultaneouslyWithGestureRecognizer to make those gestures work simultaneously. But there's still have some problems. The parent view have its Swipe gesture, the subview have its Pan gesture. Since I use souldRecognizeSimultaneouslyWithGestureRecognizer, sometime when I'm panning, the swipe gesture triggers. So, you know how to disable Swipe while Pan is active in this situation?

推荐答案

你必须实现UIGestureRecognizerDelegate方法:

You have to implement the UIGestureRecognizerDelegate method:

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

并添加你的控制器作为手势的代表然后,当两个手势识别器响应一个手势时,将调用此方法,在这里您可以实现您想要的应用程序逻辑。

And add your controller as the delegate of the gesture recognizers. Then, when two gesture recognizers respond to a gesture, this method will be called and here you can implement the logic you want for your app.

在界面声明中您必须键入的控制器:

In the interface declaration of the controller you have to type:

@interface testcViewController () <UIGestureRecognizerDelegate>

然后,在创建手势识别器时:

Then, when creating the gesture recognizer:

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
swipe.direction = UISwipeGestureRecognizerDirectionDown;
swipe.delegate = self;
[self.view addGestureRecognizer:swipe];

然后,最后,将此方法添加到控制器:

And then, finally, you add this method to the controller:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    BOOL shouldInteract = NO;
    //Here you decide whether or not the two recognizers whould interact.
    return shouldInteract;
}

编辑
您还可以实施

EDIT You can also implement

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;

在此处,检测您是否已经提交了子视图,并阻止了您想要的任何手势。

And here, detect if you have already presented the subviews, and block any gesture you want.

这篇关于如何阻止从superview到子视图的手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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