Search code examples
iosswiftgesture

How to keep two gestureRecognizers in one view and switch between them?


I am having trouble my UIPageViewController. What I ve done sofar: 1. On pageViewController you can swipe left, right in order to change VC 2. added lateral menu. When adding lateral menu, I created pangesture recognizer and added it to pageContentVC (so I can drag pageContentVC in order to close sidebar menu)

 var menuViewController: UIViewController! {
    didSet {
        self.exitPanGesture = UIPanGestureRecognizer()
        self.exitPanGesture.addTarget(self, action:"handleOffstagePan:")
      //  self.exitPanGesture.view?.userInteractionEnabled = false
        self.sourceViewController.view.addGestureRecognizer(self.exitPanGesture)

    }
}

Here I am setting pageContentVC to sourceVC of transitionManager:

 func resetToMainPage(index: Int!) {
    /* Getting the page View controller */
    mainPageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("MainPageViewController") as UIPageViewController
    self.mainPageViewController.dataSource = self
    self.mainPageViewController.delegate = self

    let pageContentViewController = self.viewControllerAtIndex(index)

    self.transtionManger.sourceViewController = pageContentViewController // adding swipe to the pageContentViewControlle in order to close menu

    self.mainPageViewController.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)


    self.mainPageViewController.view.frame = CGRectMake(0, 102, self.view.frame.width, self.view.frame.height)
    self.addChildViewController(mainPageViewController)
    self.view.addSubview(mainPageViewController.view)
    self.mainPageViewController.didMoveToParentViewController(self)

}
  1. After closing menu I disabled pangesture recognizer. ` var presentingP:Bool!{ didSet{ if presentingP == true { // enable the gesture recognizer only when the view of menucontroller is presented

            self.exitPanGesture.view?.userInteractionEnabled = true
        }else{
            // disable gesture recognizer when menu is not presented
            self.exitPanGesture.view?.userInteractionEnabled = false
    
            isMenuVisible = false
        }
    }
    

    } ` Sofar, I can swipe left, and right in order to change VC. The problem is that I can't scroll tableView on my VC. After I disabled pageContentVC gesture, everything become disable except swiping right and left (to change VC). Question: How to keep two gestureRecognizers in one view and switch between them?


Solution

  • You can use the enabled property of UIGestureRecognizer to disable the one you don't want to use and enable the other one.

    UIGestureRecognizer documentation