iPad 应用程序的 UIScrollview 中的两指轻扫

Two finger swipe in UIScrollview for iPad application(iPad 应用程序的 UIScrollview 中的两指轻扫)

本文介绍了iPad 应用程序的 UIScrollview 中的两指轻扫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我想在 UIScrollview 中实现左右滑动.我有内容大小(768,1500)的滚动视图.我已经尝试过了,但问题是有时它没有检测到滑动并在那里执行滚动.所以现在我想禁用两指触摸滚动.

Actually i want to implement swipe left and right in UIScrollview. i have scrollview with content size (768,1500). i have tried this but problem is that sometimes its not detecting swipe and perform scrolling there. so now i want to disable scrolling on 2 finger touch.

swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextswipedScreen:)] autorelease];
swipeGesture.numberOfTouchesRequired=2;
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;         
[self addGestureRecognizer:swipeGesture];

swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(previousswipedScreen:)] autorelease];
swipeGesture.numberOfTouchesRequired=2;
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;            
[self addGestureRecognizer:swipeGesture]; 

我已经为此尝试了自定义滚动视图,但 touchesBegan 方法有问题.它不是每次都打电话.即使我尝试了这个但无法停止 UIScrollview 中的两个手指滚动.

i have tried custom scrollview for that but i have problem with touchesBegan method. its not calling every time. even i tried this but not able to stop two finger scroll in UIScrollview.

for (UIGestureRecognizer *mgestureRecognizer in _scrollView.gestureRecognizers) {     
        if ([mgestureRecognizer  isKindOfClass:[UIPanGestureRecognizer class]])
        {
            UIPanGestureRecognizer *mpanGR = (UIPanGestureRecognizer *) mgestureRecognizer;
            mpanGR.minimumNumberOfTouches = 1; 
            mpanGR.maximumNumberOfTouches = 1;
        }
    }

如果您对此有任何解决方案或替代方案,请告诉我.

Let me know if you have any solution or alternative for that.

推荐答案

我也遇到了同样的问题;我需要禁用两指滚动,以便检测到向左或向右滑动的两指.这是我设置滚动视图时所做的:

I had the same problem; I needed to disable two-finger scrolling so that I could detect a two-finger swipe to the left or right. Here's what I did to set up my scroll view:

- (void) setUpGestureHandlersOnScrollView:(UIScrollView *)scrollView {
    // set up a two-finger pan recognizer as a dummy to steal two-finger scrolls from the scroll view
    // we initialize without a target or action because we don't want the two-finger pan to be handled
    UIPanGestureRecognizer *twoFingerPan = [[UIPanGestureRecognizer alloc] init];
    twoFingerPan.minimumNumberOfTouches = 2;
    twoFingerPan.maximumNumberOfTouches = 2;
    [scrollView addGestureRecognizer:twoFingerPan];

    // set up the two-finger left and right swipe recognizers
    UISwipeGestureRecognizer *twoFingerSwipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGestureFrom:)];
    twoFingerSwipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    twoFingerSwipeLeft.numberOfTouchesRequired = 2;
    [scrollView addGestureRecognizer:twoFingerSwipeLeft];

    UISwipeGestureRecognizer *twoFingerSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGestureFrom:)];
    twoFingerSwipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    twoFingerSwipeRight.numberOfTouchesRequired = 2;
    [scrollView addGestureRecognizer:twoFingerSwipeRight];

    // prevent the two-finger pan recognizer from stealing the two-finger swipe gestures
    // this is essential for the swipe recognizers to work
    [twoFingerPan requireGestureRecognizerToFail:twoFingerSwipeLeft];
    [twoFingerPan requireGestureRecognizerToFail:twoFingerSwipeRight];
}

处理程序方法应如下所示:

The handler method should look something like this:

- (void)handleGestureFrom:(UISwipeGestureRecognizer *)recognizer {
    if ([recognizer numberOfTouches] == 2) {
        // do whatever you need to do
    }
}

这篇关于iPad 应用程序的 UIScrollview 中的两指轻扫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iPad 应用程序的 UIScrollview 中的两指轻扫

基础教程推荐