简体   繁体   中英

How can I set UIPageViewController (Paging Type) gestures delegate

I have been trying to fix conflicts between UIPageViewControllers's gestures and the ones added on views and UIViewControllers lying inside the page controllers. I simply need to make UIPageViewController a delegate for its gestures how can I do this?

  1. I have conformed and implemented the UIGestureDelegate methods in page view, but they are never called.
  2. I have also looped to check if gestures exists in UIPageViewControllers gestureRecognizers property. As expected, this array was nil, as mentioned in docs "..Only populated if transition style is 'UIPageViewControllerTransitionStylePageCurl'."
  3. I tried fetching the underlying UIScrollView and set its pan gestures delegate to UIPageViewController. Was unable to do so.

How can I set the UIPageViewController gesture's (specifically Pan Gesture) delegate.

This does not set the gesture delegate for the built in Pan gesture of the UIPageViewController but I some how managed to resolve gesture conflicts by fetching the underlying scrollview and then setting a new Pan gesture on the scroll view. This new Pan gesture has the UIPageViewController as its delegate and hence I am able to get calls for the gesture delegate.

-(UIScrollView *)scrollViewInsidePageControl {
    for (UIView * view in self.view.subviews) {
        if([view isKindOfClass:[UIScrollView class]])
            return (UIScrollView *)view;
    }
    return nil;
}

And in -viewDidLoad:

self.internalScrollView = [self scrollViewInsidePageControl];
self.customPanGesture = [UIPanGestureRecognizer new];
self.customPanGesture.delegate = self;
[self.internalScrollView addGestureRecognizer:_customPanGesture];

The above code made calls on the protocol methods for UIGestureRecognizerDelegate on the UIPageViewController instance

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if(otherGestureRecognizer.view == self.internalScrollView)
        return NO;
    else
        return YES;
}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer == _customPanGesture)
    {
        // Did required handling here
    }
    return YES;
}

To set UIPageViewController delegate, simply add "UIPageViewControllerDelegate" and "UIPageViewControllerDataSource" to your UIPageViewController. Then set the delegate and the dataSource of your UIPageViewController to self (the delegate).

class MyPageViewController : UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
  override func viewDidLoad() {
     super.viewDidLoad()
     self.dataSource = self
     self.delegate = self
   }
}

If you do not set the dataSource, you will not be able to have the UIPageViewController slide gestures between the views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM