【问题标题】:How to recognize swipe gesture in UIScrollView如何识别 UIScrollView 中的滑动手势
【发布时间】:2011-04-08 14:49:00
【问题描述】:

我正在尝试识别UIScrollView 中的左/右滑动手势。我尝试创建UISwipeGestureRecognizers 并将它们与滚动视图相关联。它有效,但很少。大多数时候我没有接到电话。为什么?

我怎样才能可靠地向左/向右滑动来工作?我可以使用手势识别器还是我必须自己在touchesBegan/Ended中处理它

谢谢

【问题讨论】:

    标签: iphone uiscrollview gesture swipe


    【解决方案1】:

    对于那些想要动画和自定义他们的滑动手势识别器的人。

    我们可以使用 UIScrollView 和 UIGestureRecognizer 委托:

     Class ViewController: UIViewController, UISCrollViewDelegate, UIGestureRecognizerDelegate { 
    
    
       override func viewDidLoad() {
        super.viewDidLoad()
    
        scrollView.delegate = self
        swipeLeft.delegate = self
        swipeRight.delegate = self
    
      }
    
    
      func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return true
      }
    
      func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
      }
    
      func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return scrollView.alwaysBounceHorizontal
      }
    
    
      func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    
        // Your custom animation at the end of scrolling.
      }
    }
    

    【讨论】:

      【解决方案2】:
      UIScrollView *scrollView = ...
      UISwipeGestureRecognizer *mySwipe = ...
      

      解决这个问题的正确方法是添加一行代码:

      [scrollView.panGestureRecognizer requireGestureRecognizerToFail:mySwipe]
      

      Swift 版本:

      scrollView.panGestureRecognizer.requireGestureRecognizerToFail(mySwipe)
      

      Swift4 版本:

      scrollView.panGestureRecognizer.require(toFail: mySwipe!);
      

      【讨论】:

      • 绝对同意这是正确的解决方案!我还必须在同一行中添加 pinchGestureRecognizer 以使其可靠,因为我试图检测多指滑动。
      • 这绝对是正确的方法!有时我发现滑动手势触发得太快了,但现在这是一个滑动处理程序问题。至少我们不必做其他人建议的各种奇怪的骇人听闻的事情,而只是“感觉不对”。谢谢!!
      • 有史以来最棒的答案.. :)
      • 这不是简单地打破了滚动视图在滑动手势方向上的平移手势吗?至少,我似乎无法让滑动手势失败。如果您希望能够平移放大的图像,但也可以在图像之间滑动,该怎么办?似乎接受的解决方案更好,即使它可能是“hacky”。​​
      • @nickjwallin nope.
      【解决方案3】:

      想通了。就我而言,我的 UIScrollView 包含一个允许缩放的 UIImage。显然,这意味着启用了滚动,并且 UIScrollView 无法区分旨在滚动和滑动的手势(下一个、上一个图像)。

      在我的例子中,关键是在图像未放大时禁用滚动视图中的滚动,并在放大时重新启用它。这提供了预期的行为。

      关键是在滚动视图的委托中添加以下内容:

      - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
        if (scrollView.zoomScale!=1.0) {
          // Zooming, enable scrolling
          scrollView.scrollEnabled = TRUE;
        } else {
          // Not zoomed, disable scrolling so gestures get used instead
          scrollView.scrollEnabled = FALSE;
        }
      }
      

      我还必须在禁用滚动的情况下初始化滚动视图。 要启用缩放,只需在委托调用中提供图像,

      - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
        // Return the scroll view
        return myImage;
      }
      

      并在 viewDidLoad 中为缩放和设置手势识别器设置一些参数

      - (void)viewDidLoad {
        [super viewDidLoad];
        myScrollView.contentSize = CGSizeMake(myImage.frame.size.width, myImage.frame.size.height);
        myScrollView.maximumZoomScale = 4.0;
        myScrollView.minimumZoomScale = 1.0;
        myScrollView.clipsToBounds = YES;
        myScrollView.delegate = self;
      
        [myScrollView addSubview:myImage];
        [self setWantsFullScreenLayout:TRUE];
      
        myScrollView.scrollEnabled = FALSE; 
        UISwipeGestureRecognizer *recognizer = 
          [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
        recognizer.delaysTouchesBegan = TRUE;
        [myScrollView addGestureRecognizer:recognizer];
        [recognizer release];
      
        recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
        recognizer.direction = UISwipeGestureRecognizerDirectionLeft;
        [myScrollView addGestureRecognizer:recognizer];
        [recognizer release];
        [myScrollView delaysContentTouches];
      }
      

      【讨论】:

      • 真的很棒找到大卫。我的滑动识别器也无法正常工作。我放弃并使用了 touchesBegan: 和 NSNotification,但它也为 MasterViewControllers 提供了通知(在 iPad 中)。我对搜索该问题感到眼花缭乱。你节省了我的时间和精力!!衷心感谢大卫。继续发布技巧……祝你有美好的一天。
      • 或者在一行中 - scrollView.scrollEnabled = (scale != 1.0f)
      • 谢谢!当我在风景中遇到问题时,我会提出一个小建议。我通过将 scrollViewDidZoom 方法更改为这样绕过了这些问题:'- (void)scrollViewDidZoom:(UIScrollView *)scrollView { if (scrollView.zoomScale!=self.scrollview.minimumZoomScale) { // 缩放,启用滚动 scrollView.scrollEnabled = TRUE ; } else { // 不缩放,禁用滚动,以便使用手势 scrollView.scrollEnabled = FALSE; } }'
      • 下面使用 requireGestureRecognizerToFail 的新答案要容易得多。
      【解决方案4】:

      好帖子。

      我正在做类似的事情(没有图像视图),如果 contentSize 小于高度,我基本上必须禁用滚动(我的滚动视图只能垂直滚动)。

      if (scrollView.contentSize.height>scrollView.frame.size.height) {
          scrollView.scrollEnabled = YES;
      }
      else {
          scrollView.scrollEnabled = NO;
      }
      

      这对我有用

      【讨论】:

        猜你喜欢
        • 2015-08-16
        • 1970-01-01
        • 1970-01-01
        • 2014-07-20
        • 1970-01-01
        • 1970-01-01
        • 2016-08-04
        • 1970-01-01
        相关资源
        最近更新 更多