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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...