【问题标题】:Enable UIScrollView scrolling for particular CGPoint on UISwipeGestureRecognizer为 UISwipeGestureRecognizer 上的特定 CGPoint 启用 UIScrollView 滚动
【发布时间】:2015-01-28 08:43:47
【问题描述】:

我只想在从屏幕边界滑动时启用 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 中添加了滚动视图。

任何帮助将不胜感激。

【问题讨论】:

    标签: ios iphone uiscrollview uiswipegesturerecognizer swipe-gesture


    【解决方案1】:

    UIScrollView 里面已经有 UIGestureRecognizer 了。 我认为您需要将您的代码连接到那些手势识别器。您可以通过myScrollView.panGestureRecognizermyScrollView.pinchGestureRecognizer 访问它们。

    【讨论】:

    • 很抱歉打扰你,但请你用一些小演示代码来展示你的想法..
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 2016-08-24
      • 1970-01-01
      • 2012-10-04
      • 2020-03-15
      • 2018-01-31
      • 1970-01-01
      相关资源
      最近更新 更多