UIScrollView cancels UIPageViewController gestures when scrolling

I have a UIPageViewController that processes the pages of my book. However, each page of the book is a ViewController with a UIScrollView as a subview . UIScrollView has the ability to scroll only vertically due to contentSize . The problem is that while the user scrolls scrollview vertically, as scrollview continues to scroll / slow down, the user cannot rotate the page. A very easy way to see this is to scroll through the page and then try to touch the edge of the view. Usually this changes the page, and it changes the page when the scrollview not moving. However, when it moves, a short press causes the scrollview to stop moving, rather than turning the page.

How do I cancel scrollviews if a UIPageViewController tries to use this gesture to turn a page by clicking or panning a page to trigger a page rotation animation?

For an example of the behavior I want to achieve, check out the official Twitter client on the iPhone. You can scroll from the timeline to open while the channel is still slowing down from scrolling.

+4
source share
3 answers

A UIScrollView scroll event blocks other UIView animations, so in the case of Twitter, they probably UIView a second before scrolling. As you asked in your question:

"How do I cancel scrollviews if the UIPageViewController tries to use this gesture to rotate the page by clicking or panning the page to trigger a page rotation animation?"

I suggest a workaround.

Instead of relying on the UIPageViewController inherent UIPanGestureRecognizer , include your own UIPanGestureRecognizer in the page view so that when the panorama is detected in the appropriate section of the page and executed in the appropriate direction, this new UIPanGestureRecognizer overrides the UIPageViewController UIGestureRecognizer and starts. In particular, you need to:

(1) Stop the scroll animation with

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

(2) Programmatically rotate the page using

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

so that both scroll animations are stopped and page turning is completed in a single fluid gesture.

+1
source

UIGestureRecognizer class has the ability to set dependencies on other gesture recognizers using the requireGestureRecognizerToFail: method.
In your case, this method can be used like this:

 for (UIGestureRecognizer *gestureRecognizer in pageController.gestureRecognizers) { for (ViewController *viewController in viewControllers) { for (UIGestureRecognizer *gestureRecognizerForFail in viewController.scrollView.gestureRecognizers) { [gestureRecognizerForFail requireGestureRecognizerToFail:gestureRecognizer]; } } } 
0
source

I had the same problem and went my way ... in my case, I have a PDF with zoom enabled. Therefore, I have, for example:

 [scrollView setMaximumZoomScale:6]; [scrollView setMinimumZoomScale:1]; 

when I initialize the controller and its scrollView, and immediately after that, and every time I change the orientation or page, I change the scale to fit the width of the page only if the scale is "deleted"

 CGFloat desiredWidth = scrollView.frame.size.width/pdfRect.size.width; if (desiredWidth>[self zoomScale]) { [scrollView setZoomScale:desiredWidth animated:YES]; } 

I hope this helps

-1
source

Source: https://habr.com/ru/post/1444225/


All Articles