8

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

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

更新
我使用 shouldRecognizeSimultaneouslyWithGestureRecognizer 使这些手势同时工作。但是还是有一些问题。父视图有它的滑动手势,子视图有它的平移手势。由于我使用了 souldRecognizeSimultaneouslyWithGestureRecognizer,所以有时在我平移时,会触发滑动手势。那么,您知道在这种情况下如何在 Pan 处于活动状态时禁用 Swipe 吗?

4

6 回答 6

14

您必须实现 UIGestureRecognizerDelegate 方法:

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

并将您的控制器添加为手势识别器的代表。然后,当两个手势识别器响应一个手势时,将调用此方法,您可以在此处为您的应用实现所需的逻辑。

在控制器的接口声明中,您必须键入:

@interface testcViewController () <UIGestureRecognizerDelegate>

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

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

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

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

编辑 您还可以实施

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

在这里,检测您是否已经呈现了子视图,并阻止您想要的任何手势。

于 2013-06-07T09:07:40.350 回答
2

将 userinteractionEnabled 设置为子视图的 NO

 subview.userinteractionEnabled=NO

如果你不想禁用 userInteraction 然后使用cancelsTouchesInView方法

cancelsTouchesInView——如果一个手势识别器识别出它的手势,它会从他们的视图中解除该手势的剩余触摸(所以窗口不会传递它们)。窗口使用 (touchesCancelled:withEvent:) 消息取消先前传递的触摸。如果手势识别器无法识别其手势,则视图将接收多点触摸序列中的所有触摸。

于 2013-06-07T08:40:28.487 回答
2

为了阻止来自超级视图的所有手势识别器,我创建了一个 UIGestureRecognizer 子类,它在附加到视图时会执行此操作。请参阅以下代码(取自我的 WEPopover 项目):

#import "WEBlockingGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>

@implementation WEBlockingGestureRecognizer

- (id)init {
    return [self initWithTarget:self action:@selector(__dummyAction)];
}

- (id)initWithTarget:(id)target action:(SEL)action {
    if ((self = [super initWithTarget:target action:action])) {
        self.cancelsTouchesInView = NO;
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if (self.state == UIGestureRecognizerStatePossible) {
        self.state = UIGestureRecognizerStateBegan;
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    self.state = UIGestureRecognizerStateRecognized;
}

- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer {
    return [self isGestureRecognizerAllowed:preventingGestureRecognizer];
}

- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer {
    return ![self isGestureRecognizerAllowed:preventedGestureRecognizer];
}

- (BOOL)shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return ![self isGestureRecognizerAllowed:otherGestureRecognizer];
}

- (BOOL)shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return NO;
}

- (BOOL)isGestureRecognizerAllowed:(UIGestureRecognizer *)gr {
    return [gr.view isDescendantOfView:self.view];
}

- (void)__dummyAction {

}

@end

如果要阻止附加到某个视图的父视图的所有手势识别器,只需执行以下操作:

- (void)blockParentGesturesForView:(UIView *)v {
    [v addGestureRecognizer:[WEBlockingGestureRecognizer new]];
}
于 2014-09-18T14:52:17.280 回答
1

试试这样

   - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return NO;
}
于 2013-06-07T08:44:58.997 回答
1

考虑到我有一个dialogView作为我UIViewController的 main的直接子视图,我view将一个手势识别器附加到 mainview并执行以下操作(将我的视图控制器设置为手势识别器delegate)

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    let point = touch.location(in: view)
    return !dialogView.frame.contains(point)
} 
于 2018-02-14T15:34:31.420 回答
0

拿了我在这里读到的东西,制作了一个很棒的 Swift 5(对我来说)!我在一个视图中有一个滑动控件,该控件也有一个用于移动到另一个视图的滑动识别器。如果手指没有碰到滑块的拇指,那么整个视图都会移动。

通过将此视图放置在滑块下方(扩展使其更大一些),未命中不会做任何事情。

final class BlockingView: UIView, UIGestureRecognizerDelegate {
    let swipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer();

    init() {
        super.init(frame: CGRect.zero)

        swipe.direction = [.left, .right]
        self.addGestureRecognizer(swipe)
    }
    required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }

    @objc override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { return false }
}

希望这可以为某人省去麻烦!

于 2019-11-06T21:54:37.337 回答