如何识别UIScrollView中的滑动手势

时间:2010-09-06 04:15:26

标签: iphone uiscrollview gesture swipe

我正在尝试识别UIScrollView中的左/右滑动手势。我试图创建UISwipeGestureRecognizers并将它们与滚动视图相关联。它的工作原理很少见。大部分时间我都没有被召唤。为什么呢?

如何可靠地左/右滑动工作?我可以使用手势识别器,还是我必须以touchesBegan/Ended

自己处理它

由于

4 个答案:

答案 0 :(得分:41)

想出来。在我的例子中,我的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];
}

答案 1 :(得分:29)

UIScrollView *scrollView = ...
UISwipeGestureRecognizer *mySwipe = ...

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

[scrollView.panGestureRecognizer requireGestureRecognizerToFail:mySwipe]

Swift版本:

scrollView.panGestureRecognizer.requireGestureRecognizerToFail(mySwipe)

Swift4版本:

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

答案 2 :(得分:4)

好帖子。

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

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

这对我有把戏

答案 3 :(得分: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.
  }
}