Searched a lot for this one, but couldn\'t find a proper solution yet.
Is it possible to disable the bounce effect of a UIPageViewController
and still
If you will try to disable bounce for UIPageViewController.scrollView
, you will definitely get a broken pageViewController
: swipe ain't gonna work. So, don't do that:
self.theScrollView.alwaysBounceHorizontal = NO;
self.theScrollView.bounces = NO;
Use the solution with searching scrollView
reference in UIPageViewController
subviews only for disabling scroll entirely:
@interface MyPageViewController : UIPageViewController
@property (nonatomic, assign) BOOL scrollEnabled;
@end
@interface MyPageViewController ()
@property (nonatomic, weak) UIScrollView *theScrollView;
@end
@implementation MyPageViewController
- (void)viewDidLoad
{
[super viewDidLoad];
for (UIView *view in self.view.subviews) {
if ([view isKindOfClass:UIScrollView.class]) {
self.theScrollView = (UIScrollView *)view;
break;
}
}
}
- (void)setScrollEnabled:(BOOL)scrollEnabled
{
_scrollEnabled = scrollEnabled;
self.theScrollView.scrollEnabled = scrollEnabled;
}
@end
UIScrollView
category (for ex. CustomScrolling). UIScrollView
is delegate of their gesture recognizer already.UIViewController
(aka baseVC
with UIPageViewController
inside) shared via AppDelegate
. Otherwise you can use run-time (#import
) and add reference property (to your controller baseVC
) to the category.Implement category:
@interface UIScrollView (CustomScrolling)
@end
@implementation UIScrollView (CustomScrolling)
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
UIViewController * baseVC = [(AppDelegate *)[[UIApplication sharedApplication] delegate] baseVC];
if (gestureRecognizer.view == baseVC.pageViewController.theScrollView) {
NSInteger page = [baseVC selectedIndex];
NSInteger total = [baseVC viewControllers].count;
UIPanGestureRecognizer *recognizer = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint velocity = [recognizer velocityInView:self];
BOOL horizontalSwipe = fabs(velocity.x) > fabs(velocity.y);
if (!horizontalSwipe) {
return YES;
}
BOOL scrollingFromLeftToRight = velocity.x > 0;
if ((scrollingFromLeftToRight && page > 0) || (!scrollingFromLeftToRight && page < (total - 1))) {
return YES;
}
return NO;
}
return YES;
}
@end
Import category file #import "UIScrollView+CustomScrolling.h"
in your baseVC
, that uses UIPageViewController.