UIScrollView cancels UIPageViewController gestures when scrolling

前端 未结 3 624
死守一世寂寞
死守一世寂寞 2021-01-13 14:47

I have a UIPageViewController that handles turning the pages of my \"book\". However, each book page is a ViewController with a UIScrollView

3条回答
  •  不要未来只要你来
    2021-01-13 14:56

    A UIScrollView scroll event will block other UIView animations, so in the case of Twitter, they're probably canceling the scroll a split second before swiping the view. As you've asked in your question:

    "How do I cancel the scrollviews gestures if the UIPageViewController is trying to use the gesture to turn the page by tapping or panning the page to cause the page turn animation?"

    I'll suggest a workaround.

    Instead of relying on the UIPageViewController's inherent UIPanGestureRecognizer, include your own UIPanGestureRecognizer in the page view so that when a pan is detected in the appropriate section of the page and performed in the appropriate direction, that new UIPanGestureRecognizer overrides the UIPageViewController's UIGestureRecognizers and triggers the necessary actions. Specifically, you need to:

    (1) Halt the scrolling animation using

    CGPoint offset = scrollView.contentOffset;
    [scrollView setContentOffset:offset animated:NO];
    

    (2) Turn the page programmatically using

    - (void)setViewControllers:(NSArray *)viewControllers direction:
      (UIPageViewControllerNavigationDirection)direction animated:
      (BOOL)animated completion:(void (^)(BOOL finished))completion;
    

    so that both the scrolling animation is halted and the page flip is completed within one fluid pan gesture.

提交回复
热议问题