水平滚动的UIScrollView垂直平移手势(Horizontal scrolling UIScr

2019-08-01 02:16发布

我试图实现一个UIScrollView水平在那里通过图片中滚动视图平移滚动,但垂直平移执行其他操作我有。 现在我有一个分页禁用垂直滚动,对于通过图片滚动的作品就好了一个UIScrollView,但我要疯了试图找到一种方式来拦截垂直平底锅和打电话给我自己的方法,而不是垂直锅只是被吃掉的滚动视图。 我能做到

    UIPanGestureRecognizer *panUp = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[self.scrollView addGestureRecognizer:panUp];
[scrollView.panGestureRecognizer setEnabled:NO];

这使我在我的handlePanGesture平移总量控制:方法,但因为当时我觉得我基本上会被重写所有我试图避免滚动的逻辑,这是不是特别有帮助。 关于如何实现这一目标的任何帮助或指导将不胜感激。

谢谢!

Answer 1:

我认为,如果你想使用,因为滚动视图的使用这些手势的移动手势,这将是困难的。 但是,如果你可以使用刷卡,很容易实现。

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    swipe.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
    [self.scrollView addGestureRecognizer:swipe];

我测试了这一点,它工作得很好调用处理程序,你不需要禁用滚动视图的泛手势识别(在我的测试中,滚动视图是身高不够,内容足够短,没有垂直滚动 - - 我不知道这是否是必要的或没有)。



Answer 2:

随着iOS 5的中, UIScrollView 暴露其UIPanGestureRecognizer 。 设置自定义刷卡的delegate ,以self ,使gestureRecognizer属性或伊娃,使你的类符合<UIGestureRecognizerDelegate>的协议。 然后,实施UIGestureRecognizerDelegate– gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:像这样:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([gestureRecognizer isEqual:self.swipe] && [otherGestureRecognizer isEqual:self.scrollView.panGestureRecognizer])
    {
         return NO;
    }
    return YES; // the default for this method
}


Answer 3:

我加了UIPanGestureRecognizer然后设置gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:是这样的:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ([gestureRecognizer isEqual:self.panGesture] && [otherGestureRecognizer isEqual:self.collectionView.panGestureRecognizer]){
        return YES;
    }
    return NO;
}

我还添加了一个检查gestureRecognizerShouldBegin:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ([gestureRecognizer isEqual:self.panGesture]) {
        if (gestureRecognizer.numberOfTouches > 0) {
            CGPoint point = [gestureRecognizer locationOfTouch:0 inView:gestureRecognizer.view];
            CGFloat distX = abs(self.collectionView.lastTouchPos.x - point.x);
            CGFloat distY = abs(self.collectionView.lastTouchPos.y - point.y);
            if (distX > distY) {
                return NO;
            }
        } else {
            return NO;
        }
    }
    return YES;
}

只使用self.panGesture或向下扫视时。

更新

https://stackoverflow.com/a/8603839/667834似乎有一个更好的方法gestureRecognizerShouldBegin

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ([gestureRecognizer isEqual:self.panGesture]) {
        if (gestureRecognizer.numberOfTouches > 0) {
            CGPoint translation = [self.panGesture velocityInView:self.collectionView];
            return fabs(translation.y) > fabs(translation.x);
        } else {
            return NO;
        }
    }
    return YES;
}


文章来源: Horizontal scrolling UIScrollView with vertical pan gesture