I think I solved it. It feels that the solution is not the best or nicest one, but it works.
What I did:
I created a new UIPanGestureRecognizer
and added it to the imageScrollView
. This gestureRecognizer has min. and max. touches to 2. In the action method of this recognizer I enabled and disabled scrolling of the imageScrollView.
Also I created a own subclass of UIScrollView
and implemented the gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer
to return YES
.
In code:
MenuViewController
: My first UIViewController in the pageVC. The class is doing nothing (for the purpose of this problem). This is only a placeholder to swipe with two fingers and have something to show.
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
: I created a subclass for the UIScrollView with the images.
@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;
}