Disable UIPageViewController bounce

前端 未结 13 2380
离开以前
离开以前 2020-11-27 04:08

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

13条回答
  •  时光取名叫无心
    2020-11-27 04:34

    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
    

    Solution for disabling bounce at UIPageViewController:

    1. Create UIScrollView category (for ex. CustomScrolling). UIScrollView is delegate of their gesture recognizer already.
    2. Be aware that your target 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.
    3. 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
      
    4. Import category file #import "UIScrollView+CustomScrolling.h" in your baseVC, that uses UIPageViewController.

提交回复
热议问题