如何识别 UIScrollView 中的滑动手势

时间:2022-10-16
本文介绍了如何识别 UIScrollView 中的滑动手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I'm trying to recognize left/right swipe gesture in a UIScrollView. I've tried to create UISwipeGestureRecognizers and associate them with the scroll view. It works but very rarely. Most of the time I do not get called. Why?

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

How can I reliably get swiping left/right to work? Can I use the gesture recognizers or do I have to somehow handle it myself in touchesBegan/Ended

谢谢

推荐答案

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

Figured it out. In my case, my UIScrollView contained a UIImage that I allowed zooming. Apparently that meant that scrolling is enabled and the UIScrollView had trouble distinguishing between gestures intended to scroll vs. swipe (next, previous image).

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

The key in my case, is to disable scrolling in the scroll view when the image is not zoomed in, and renabled it when it is zoomed in. This provides the expected behavior.

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

The critical piece is to put the following in the scroll view's delegate:

- (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;
  }
}

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

I also have to initialize the scroll view with scrolling disabled. To enable zooming, simply provide an image on a delegate call,

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

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

And set a few parms in viewDidLoad for the zooming and setup gesture recognizers as well

- (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];
}

这篇关于如何识别 UIScrollView 中的滑动手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:拖动按钮时ScrollView不滚动 下一条:viewWillAppear: 和 viewDidAppear: 之间的视图框架变化

相关文章

最新文章