ios – 仅捕获UIView 2手指UIPanGestureRecognizer

前端之家收集整理的这篇文章主要介绍了ios – 仅捕获UIView 2手指UIPanGestureRecognizer前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的视图控制器中有几个UIScrollViews.我想覆盖一个视图,通过UIPanGestureRecognizer捕获2手指滑动,这不会记录UIScrollView滑动手势.

当我用2手指平移手势对我的内容进行透明的视图时,我的水龙头和1个手指滑动不被检测到.我试图覆盖pointInside:方法返回NO
但是它不记录我的2手指滑动.

效果类似于4手指滑动来更改应用程序.

解决方法

您不需要重叠视图.
首先实现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处理2手指平移与UIScrollView平移手势

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

猜你在找的iOS相关文章