2010-09-06 34 views
22

我试图识别UIScrollView中的左/右轻扫手势。我试图创建UISwipeGestureRecognizers并将它们与滚动视图关联。它有效,但很少。大多数时候我都不会打电话。为什么?如何在UIScrollView中识别轻扫手势

我该如何可靠地刷左/右工作?我可以使用手势识别器或我必须以某种方式处理它自己在touchesBegan/Ended

感谢

回答

38

想通了。在我的情况下,我的UIScrollView包含一个UIImage,允许缩放。显然这意味着滚动被启用,并且UIScrollView无法区分意图滚动与滑动(下一个,前一个图像)的手势。

在我的情况下,关键是禁用图像放大时滚动视图中的滚动,并在放大时对其进行renable。这提供了预期的行为。

的关键部分是放在滚动视图的委托执行以下操作:

- (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中的几个PARMS的缩放和设置手势识别以及

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

真的很棒找到大卫。我的滑动识别器也无法工作。我放弃了并使用了touchesBegan:使用NSNotification,但它也给了MasterViewControllers(在iPad中)的通知。我对模糊的眼光寻找这个概率。你节省了我的时间和精力!衷心感谢大卫。继续发布技巧..祝你有个美好的一天。 – gopikrishnan 2010-12-08 21:56:26

+0

感谢您的技巧 – iOSAppDev 2012-06-27 12:48:59

+0

或者也许在一行 - scrollView.scrollEnabled =(scale!= 1.0f) – Yariv 2013-02-22 09:11:44

4

好贴。

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

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

这奏效了,我

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

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

[scrollView.panGestureRecognizer requireGestureRecognizerToFail:mySwipe] 

斯威夫特版本:

scrollView.panGestureRecognizer.requireGestureRecognizerToFail(mySwipe) 
+0

绝对认同这是正确的解决方案!我还必须在'pinchGestureRecognizer'中添加相同的行,以便在我尝试检测多指滑动时使其可靠。 – 2015-04-24 10:51:21

+0

这绝对看起来是正确的路!有时候我发现滑动手势过早燃烧,但现在这是一个滑动处理程序问题。至少我们不必做其他人建议的各种奇怪的黑客事情,只是“感觉不对”。谢谢!! – horseshoe7 2015-12-14 16:24:18

+1

所有时间的伟大答案.. :) – 2016-02-11 12:48:50

相关问题