在UISwipeGestureRecognizer上为特定的CGPoint启用UIScrollView滚动



我想仅在从屏幕边界滑动时才启用UIScrollView的滚动。我正在使用滑动手势识别器。

这是我的代码

-(void)swipeHandlerLeft:(UISwipeGestureRecognizer *)swipeRecognizer
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    point =  [swipeRecognizer locationInView:self.view];
    if(swipeRecognizer.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        if (point.x > screenWidth - 10)
        {
            NSLog(@"Swipped Left");
            _scrollView.scrollEnabled = YES;
        }
        else
        {
            _scrollView.scrollEnabled = NO;
        }
    }
}
-(void)swipeHandlerRight:(UISwipeGestureRecognizer *)swipeRecognizer
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    point =  [swipeRecognizer locationInView:self.view];
    if(swipeRecognizer.direction == UISwipeGestureRecognizerDirectionRight)
    {
        if (point.x < 10)
        {
            NSLog(@"Swipped Right");
            _scrollView.scrollEnabled = YES;
        }
        else
        {
            _scrollView.scrollEnabled = NO;
        }
    }    
}

上面代码的问题是它可以工作到未启用滚动视图,但是一旦启用它,就不会调用此方法,我认为原因是因为我在我的 UIView 中添加了滚动视图。

任何帮助将不胜感激。

UIScrollView内部已经有UIGestureRecognizer。我认为您需要将代码连接到这些手势识别器。您可以通过 myScrollView.panGestureRecognizermyScrollView.pinchGestureRecognizer .

您的滚动视图在滑动视图之前捕获您的触摸,我想滑动手势识别器位于滚动视图的超级视图上。

必须通过在 scrollView 平移手势识别器上调用 requireGestureRecognizerToFail(_:) 并传递轻扫手势来创建依赖项。

最新更新