UIPageViewController里面的UIScrollView:1和2个手指滑动手势

时间:2014-01-24 11:13:24

标签: ios

结构: 我有一个UIPageViewController,目前包含两个UIViewController(称为MenuVCImageScrollVC)。 在第二个UIViewController(ImageScrollVC)中,我有一个UIScrollView。此UIScrollView包含多个图像。用户应该能够在图像之间滑动。

我想要

  • UIPageViewController只应在用户使用两个手指时滚动 **因此,用户可以随时用两根手指在“MenuVC”和ImageScrollVC之间滑动。
  • UIScrollView只应在用户使用一根手指时滚动 **使用单指轻扫手势,用户可以更改图像。

这可能吗?

我的假设是将双指滑动手势转发到基础UIPageViewController,但我不知道该怎么做。

更新

我的ViewController结构:

RootViewController正在实现UIPageViewController(与Apple模板完全相同)。 MenuViewController是PageViewController中的第一个ViewController,ImageScrollViewController是第二个带有UIScrollView的图像。

enter image description here

2 个答案:

答案 0 :(得分:0)

你的假设是正确的。通常,嵌套UIScrollView中的ImageScrollVC将同时处理单指和双指滑动。您可以在UIScrollView中使用ImageScrollVC的自定义子类忽略双指滑动,以便它们将继续向上链并由UIPageViewController处理。有关详细信息,请查看以下示例代码所基于的related question

它的要点是你将在-gestureRecognizerShouldBegin:子类中实现UIScrollView;如果超类返回YES,并且触摸次数为1,则仅返回YES

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)recognizer
{
    return recognizer.numberOfTouches == 1 && [super gestureRecognizerShouldBegin:recognizer];
}

答案 1 :(得分:0)

我想我解决了。感觉解决方案不是最好或最好的解决方案,但它确实有效。

我做了什么: 我创建了一个新的UIPanGestureRecognizer并将其添加到imageScrollView。这个gestureRecognizer有分钟。最多触摸到2.在此识别器的操作方法中,我启用并禁用了imageScrollView的滚动。

我还创建了自己的UIScrollView子类,并实现了gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer以返回YES

代码

MenuViewController :我在pageVC中的第一个UIViewController。该课程什么都不做(出于这个问题的目的)。这只是一个占位符,可以用两根手指滑动并显示一些东西。

<强> RootViewController

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
//... exactly the same code as in the Apple template

//the UIPageViewController should only use 2 finger:
for (UIView* view in [self.pageViewController.view subviews]) {
    if ([view  isKindOfClass:[UIScrollView class]]) {
        UIScrollView* scrollView = (UIScrollView*)view;
        scrollView.panGestureRecognizer.minimumNumberOfTouches = 2;
        scrollView.panGestureRecognizer.maximumNumberOfTouches = 2;
    }
}

<强> ImageScrollViewController

//self.scrollView is from type 'CustomScrollView'
-(void)viewDidLoad {
  [super viewDidLoad];
  // autolayout would be nicer, but this was faster and easier for me
  self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * _pageImages.count, self.scrollView.frame.size.height);
  //... a for loop to add all UIImageViews as a subView to self.scrollView

  UIPanGestureRecognizer *twoFingerPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
  twoFingerPan.minimumNumberOfTouches = 2;
  twoFingerPan.maximumNumberOfTouches = 2;
  [self.scrollView addGestureRecognizer:twoFingerPan];
  twoFingerPan.delegate = (CarScrollView*)self.scrollView;
}

//the inner scrollView for the images should not scroll when two fingers are active
- (void)handlePan:(UIPanGestureRecognizer*)recognizer {
  switch (recognizer.state) {
    case UIGestureRecognizerStateBegan:
    case UIGestureRecognizerStatePossible:
        self.scrollView.scrollEnabled = NO;
        break;

    case UIGestureRecognizerStateCancelled:
    case UIGestureRecognizerStateEnded:
    case UIGestureRecognizerStateFailed:
        self.scrollView.scrollEnabled = YES;

    default:
        break;
  }
}

CustomScrollView :我使用图片为UIScrollView创建了一个子类。

@interface CarScrollView : UIScrollView <UIGestureRecognizerDelegate>

@implementation CarScrollView

//Method has to return YES so that a two finger pan gesture will be used for the outer scrollView/pageViewController
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
  return YES;
}