10

我的视图控制器中有几个UIScrollViews 。我想覆盖一个捕获 2 根手指滑动的视图,通过UIPanGestureRecognizer它不会记录UIScrollView滑动手势。

当我使用 2 指平移手势在内容上放置透明视图时,未检测到我的点击和 1 指滑动。我尝试覆盖pointInside:返回的方法,NO 但它没有记录我的 2 指滑动。

效果类似于 4 指滑动更改应用程序。

4

2 回答 2

12

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

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
                                                initWithTarget:self 
                                                        action:@selector(handlePan:)];
panGestureRecognizer.delegate = self;
panGestureRecognizer.minimumNumberOfTouches = 2;
panGestureRecognizer.maximumNumberOfTouches = 2;
[self.view addGestureRecognizer:panGestureRecognizer];

用于UIGestureRecognizerDelegate处理带有UIScrollView平移手势的 2 指平移

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

最后你可以处理 2 个手指平移

- (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");
}
于 2013-05-07T10:33:26.547 回答
3

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

- (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!");
}

当使用 2 个手指时,我会收到twoFingerPan:回电。滚动视图panGestureRecognizer此时仍在工作,因此我禁用滚动视图上的滚动以处理 2 指平移。我发现这种方法效果很好。一种奇怪的事情是,如果滚动视图正在减速,则不会调用 2 指手势识别器。希望有帮助!

于 2013-05-04T15:55:28.367 回答