如何识别UIScrollView中的滑动手势 [英] How to recognize swipe gesture in UIScrollView

查看:196
本文介绍了如何识别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中的滑动手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆