Ios 仅捕获UIView 2手指UIPangestureRecognitor

Ios 仅捕获UIView 2手指UIPangestureRecognitor,ios,objective-c,ipad,uigesturerecognizer,uipangesturerecognizer,Ios,Objective C,Ipad,Uigesturerecognizer,Uipangesturerecognizer,我的视图控制器中有两个UIScrollViews。我想覆盖一个视图,该视图通过uipangestureerecognizer捕获两个手指的滑动,该视图不会记录UIScrollView滑动手势 当我用两个手指的平移手势在我的内容上放置一个透明视图时,不会检测到我的点击和一个手指的滑动。我试图覆盖内部点:方法以返回否 但它不会记录我的两个手指滑动 这种效果类似于用4个手指滑动来更改应用程序。如果你真的不需要覆盖,你可以用手势识别器来解决这个问题。我写这篇文章是为了测试: - (void)viewDi

我的视图控制器中有两个
UIScrollView
s。我想覆盖一个视图,该视图通过
uipangestureerecognizer
捕获两个手指的滑动,该视图不会记录
UIScrollView
滑动手势

当我用两个手指的平移手势在我的内容上放置一个透明视图时,不会检测到我的点击和一个手指的滑动。我试图覆盖
内部点:
方法以返回
但它不会记录我的两个手指滑动


这种效果类似于用4个手指滑动来更改应用程序。

如果你真的不需要覆盖,你可以用手势识别器来解决这个问题。我写这篇文章是为了测试:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, self.view.bounds.size.height);

    UIView *green = [[UIView alloc] initWithFrame:self.view.bounds];
    [green setBackgroundColor:[UIColor greenColor]];

    UIView *blue = [[UIView alloc] initWithFrame:CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0)];
    [blue setBackgroundColor:[UIColor blueColor]];

    [_scrollView addSubview:green];
    [_scrollView addSubview:blue];

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPan:)];
    [pan setMinimumNumberOfTouches:2];
    [pan setMaximumNumberOfTouches:2];
    [pan setDelaysTouchesBegan:YES];

    [_scrollView addGestureRecognizer:pan];

    [self.view addSubview:_scrollView];
}

- (void)twoFingerPan:(UIPanGestureRecognizer *)gesture {
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
            self.scrollView.scrollEnabled = NO;
            break;
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateFailed:
            self.scrollView.scrollEnabled = YES;
            break;
        default:
            break;
    }
    NSLog(@"2 Fingers!");
}

我得到了twoFingerPan:当使用两个手指时的回电。滚动视图的
pangestureerecognizer
此时仍在工作,因此我禁用滚动视图以处理双指平移。我发现这个方法很有效。一种不可靠的情况是,如果滚动视图正在减速,则不会调用2指手势识别器。希望有帮助

您不需要覆盖视图。
首先实现将处理2个手指平移的
uipangestrerecognizer
,并将其分配给包含
UIScrollView
s的视图

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
                                                initWithTarget:self 
                                                        action:@selector(handlePan:)];
panGestureRecognizer.delegate = self;
panGestureRecognizer.minimumNumberOfTouches = 2;
panGestureRecognizer.maximumNumberOfTouches = 2;
[self.view addGestureRecognizer:panGestureRecognizer];
使用
UIgestureRecognitizerDelegate
使用
UIScrollView
pan手势处理双指平移

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
最后,你能用两个手指在锅里吃东西

- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
    NSLog(@"pan");
}
如果要在检测到双手指平移时停止滚动
UIScrollView
,可以禁用和启用
UIScrollView
平移识别器

- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        _scrollView.panGestureRecognizer.enabled = NO;
    }
    if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        _scrollView.panGestureRecognizer.enabled = YES;
    }
    NSLog(@"pan");
}

因此,希望您的滚动视图不捕获两次手指滑动?@AndyJacobs您希望滚动视图检测平移还是否?您想添加覆盖层,它将检测2指平移和滚动视图,以检测其他平移,对吗?如果我想错了,请澄清