28

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

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

谢谢

4

4 回答 4

42

弄清楚了。就我而言,我的 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];
}
于 2010-09-06T16:41:22.440 回答
35
UIScrollView *scrollView = ...
UISwipeGestureRecognizer *mySwipe = ...

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

[scrollView.panGestureRecognizer requireGestureRecognizerToFail:mySwipe]

斯威夫特版本:

scrollView.panGestureRecognizer.requireGestureRecognizerToFail(mySwipe)

Swift4 版本:

scrollView.panGestureRecognizer.require(toFail: mySwipe!);
于 2015-01-27T22:22:24.410 回答
4

好帖子。

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

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

这对我有用

于 2010-09-30T02:00:59.757 回答
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.
  }
}
于 2019-07-18T06:23:39.120 回答