2

I am making app like in Euro Sport. My rootviewController is pageViewController which has 3 VCs. Swiping left and right you can change the VC. Next, I added sidebarmenu. I want to add gesture recognizer to the whole rootViewController. This is how I am adding gesture:

 self.exitPanGesture = UIPanGestureRecognizer()
        self.exitPanGesture.addTarget(self, action:"handleOffstagePan:")
        self.sourceViewController.view.addGestureRecognizer(self.exitPanGesture)
        self.sourceViewController.navigationController?.view.addGestureRecognizer(self.exitPanGesture)

When I drag rootviewController tapping on navigation bar it works. But the other parts like segmentcontrol, and pageContent doesnt work.

I am setting sourceViewController here:

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

    pageContentViewController = self.viewControllerAtIndex(index)

   // self.transtionManger.sourceViewController = pageContentViewController // adding swipe to the pageContentViewControlle in order to close menu
    self.transtionManger.sourceViewController = mainPageViewController
    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)

}

So, My question is how to add gesture recognizer to the whole rootViewController, including navbar, segmentcontrol, pageviewcontrol?

Juan Catalan
  • 2,299
  • 1
  • 17
  • 23
Yestay Muratov
  • 1,338
  • 2
  • 15
  • 28

2 Answers2

2

A gesture recognizer can only be attached to one view. So the lines below mean that only navigationController?.view is getting the gesture added.

self.sourceViewController.view.addGestureRecognizer(self.exitPanGesture)
self.sourceViewController.navigationController?.view.addGestureRecognizer(self.exitPanGesture)

You can just duplicate the gesture recognizer for each view you want to have it on and then add them all that way.

self.exitPanGesture1 = UIPanGestureRecognizer()
self.exitPanGesture1.addTarget(self, action:"handleOffstagePan:")
self.sourceViewController.view.addGestureRecognizer(self.exitPanGesture1)

self.exitPanGesture2 = UIPanGestureRecognizer()
self.exitPanGesture2.addTarget(self, action:"handleOffstagePan:")
self.sourceViewController.navigationController?.view.addGestureRecognizer(self.exitPanGesture2)
keithbhunter
  • 12,258
  • 4
  • 33
  • 58
  • now, I cant do that for two reason: 1) cant access pageviewcontroller like this sourceviewcontroller.pageviewcontroller(doesnt find it), next 2) I will use this solution with different VC, some of them has pageview, others dont! – Yestay Muratov May 27 '15 at 19:20
  • I'm not sure. I found [this answer](http://stackoverflow.com/questions/4747238/can-you-attach-a-uigesturerecognizer-to-multiple-views) not too long ago, that's why I knew to make multiple gestures that all do the same thing. – keithbhunter May 28 '15 at 11:06
0

guys, I solved my problem but I got another one. In my case , I created exitGestureRecognizer. I wanted to add this to whole viewcontroller views. So, here is my code:

 for singleView in self.sourceViewController.view.subviews {
                singleView.addGestureRecognizer(exitPanGesture)
                singleView.navigationController??.view.addGestureRecognizer(exitPanGesture)
            }

For some reason, recognizer on navBar.view doesnt work. If I write code in this way:

for singleView in self.sourceViewController.view.subviews {
            singleView.addGestureRecognizer(exitPanGesture)
            singleView.navigationController??.view.addGestureRecognizer(exitPanGesture)
        }
self.sourceViewController.navigationController?.view.addGestureRecognizer(self.exitPanGesture)

Only nav bar gestures work and subview gestures dont work. If I change the order of this code. Nav bar gestures dont work but subviews gestures work. How can I add gestures to both views?

Yestay Muratov
  • 1,338
  • 2
  • 15
  • 28