用于iPad应用程序的UIScrollview中的两个手指滑动

时间:2012-01-03 11:10:54

标签: iphone uiscrollview uigesturerecognizer

实际上我想在UIScrollview中实现左右滑动。我有内容大小(768,1500)的scrollview。我试过这个但问题是有时它没有检测到滑动并在那里执行滚动。所以现在我想禁用2手指触摸滚动。

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

我已经尝试过自定义scrollview,但我有touchesBegan方法的问题。它不是每次都打电话。即便我试过这个,但也无法在UIScrollview中停止两个手指滚动。

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

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

2 个答案:

答案 0 :(得分:22)

我有同样的问题;我需要禁用双指滚动,以便我可以检测到左手或右手的双指滑动。这是我设置滚动视图所做的:

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

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

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

答案 1 :(得分:1)

您可以创建两个手势识别器,一个用于单击,一个用于双击:

UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesOne:)];
singleTapGesture.cancelsTouchesInView = NO; 
singleTapGesture.delaysTouchesEnded = NO;
singleTapGesture.numberOfTouchesRequired = 1; // One finger single tap
singleTapGesture.numberOfTapsRequired = 1;
[Scroll_view addGestureRecognizer:singleTapGesture];
[singleTapGesture release];

UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesTwo:)];
doubleTapGesture.cancelsTouchesInView = NO; 
doubleTapGesture.delaysTouchesEnded = NO;
doubleTapGesture.numberOfTouchesRequired = 1; // One finger double tap
doubleTapGesture.numberOfTapsRequired = 2;
[Scroll_view addGestureRecognizer:doubleTapGesture];
[doubleTapGesture release];

然后,来了一拳:

[singleTapGesture requireGestureRecognizerToFail : doubleTapGesture];

requireGestureRecognizerToFail Reference

最后一行,只有在双击失败时,您的单击处理程序才能工作。因此,您可以在应用程序中同时进行单击和双击。

所以你可以这样做,在“doubleTapGesture”方法中你刚刚指定了内容大小为(0,0)的scrollview,在“singleTapGesture”方法中你指定了内容大小为768,1500的scrollview。

Source knowledge