Prevent UIPageViewController's swipe from affe

2019-07-15 05:22发布

问题:

I've seen this question asked in other areas but none of the answers work in my case. I have a UIPageViewController that has a child view controller containing a UISlider.

When you go to try to move the slider, it activates the page view controller's scrolling instead. You have to tap, pause on the slider circle, and then move.

The UIPageViewController is set to the scroll type, and thus the gestureRecognizers are not set. (The header states "Only populated if transition style is 'UIPageViewControllerTransitionStylePageCurl'"). Thus I can't even get proper access to the gestures.

I've tried several things to prevent the UIPageViewController from affecting the slider, but to no avail. I've tried: 1) walking the child subtree and setting the UIScrollView's pan gesture delegate to myself (this causes a crash) 2) Add my own gesture on top, and check the y value. This will prevent the swipe, but it also eats the tap and move so the slider doesn't actually change as expected 3) Implemented a custom slider with custom gestures. The problem is I can't match the pan gesture changing the slider in the same was as the default. Since you have to implement the pan and adjust the positions yourself, it's difficult to match the real behavior.

None of these methods actually works for me. Has anyone been able to solve this? The only solution I can think of is to prevent scrolling all together, add left/right buttons, and programmatically transition pages accordingly.

回答1:

Try this code:

UIPageViewController *pageViewController = self.parentViewController;

for (UIView *scrollView in pageViewController.view.subviews) {
    if ([scrollView isKindOfClass:[UIScrollView class]]) {
        [(UIScrollView *)scrollView setDelaysContentTouches:NO];
    }
}


回答2:

You can add an UIPanGestureRecognizer to the slider with the cancelsTouchesInView attribute to false to avoid the UIPageViewController to swipe when you drag the slider :

let panGesture = UIPanGestureRecognizer()
panGesture.cancelsTouchesInView = false
yourSlider.addGestureRecognizer(panGesture)


回答3:

I think the best solution is to redesign the UI to not have components that are horizontally scrolled/swiped on a UIPageViewController. The user will already be used to performing horizontal swipes anywhere on the UIPageViewController and it could be confusing/frustrating if that behavior is changed.