垂直Scrollview内的水平平移

时间:2014-11-28 20:03:08

标签: ios uiscrollview uikit uigesturerecognizer

我的滚动视图仅限于垂直滚动。在其中我想要有一个UIPanGestureRecognizer只能识别水平平底锅的视图。

水平识别器赢得并阻止滚动视图完全滚动。

如果它检测到一个大部分水平的手势,我希望水平平移赢,否则垂直滚动应该赢。非常类似于邮箱的工作方式,或在iOS8 Mail.app

中滑动

1 个答案:

答案 0 :(得分:3)

您可以使用UIGestureRecognizerDelegate之类的gestureRecognizerShouldBegin:方法之一来指定在哪种情况下触发哪个平移手势。

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    // If the gesture is a pan, determine whether it starts out more
    // horizontal than vertical than act accordingly
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {

        UIPanGestureRecognizer *panGestureRecognizer = (UIPanGestureRecognizer *)gestureRecognizer;
        CGPoint velocity = [panGestureRecognizer velocityInView:self.view];

        if (gestureRecognizer == self.scrollView.panGestureRecognizer) {
            // For the vertical scrollview, if it's more vertical than
            // horizontal, return true; else false
            return fabs(velocity.y) > fabs(velocity.x);
        } else {
            // For the horizontal pan view, if it's more horizontal than
            // vertical, return true; else false
            return fabs(velocity.y) < fabs(velocity.x);
        }
    }
    else
        return YES;
}