如何为ViewController和它的Children添加手势识别器?

时间:2015-05-27 16:56:42

标签: ios swift uigesturerecognizer gesture

我在Euro Sport中制作应用程序。我的rootviewController是pageViewController,它有3个VC。向左和向右滑动即可更改VC。接下来,我添加了sidebarmenu。我想在整个rootViewController中添加手势识别器。这是我添加手势的方式:

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

当我拖动rootviewController轻击导航栏时,它可以工作。但是像segmentcontrol和pageContent这样的其他部分也不起作用。

我在这里设置sourceViewController:

 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)

}

所以,我的问题是如何在整个rootViewController中添加手势识别器,包括navbar,segmentcontrol,pageviewcontrol?

3 个答案:

答案 0 :(得分:1)

手势识别器只能附加到一个视图。因此,下面的行表示只有navigationController?.view才会添加手势。

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

您可以为要打开的每个视图复制手势识别器,然后以这种方式添加它们。

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)

答案 1 :(得分:0)

我还没试过这个,但在SO UITapGestureRecognizer tap on self.view but ignore subviews

上发现了类似的问题

这可能会帮助您找到解决方案。

答案 2 :(得分:0)

伙计们,我解决了我的问题,但我得到了另一个问题。就我而言,我创建了exitGestureRecognizer。我想将它添加到整个viewcontroller视图中。所以,这是我的代码:

extension UIView {

    public func removeAllConstraints() {
        var _superview = self.superview

        while let superview = _superview {
            for constraint in superview.constraints {

                if let first = constraint.firstItem as? UIView, first == self {
                    superview.removeConstraint(constraint)
                }

                if let second = constraint.secondItem as? UIView, second == self {
                    superview.removeConstraint(constraint)
                }
            }

            _superview = superview.superview
        }

        self.removeConstraints(self.constraints)
        self.translatesAutoresizingMaskIntoConstraints = true
    }
}

出于某种原因,navBar.view上的识别器无法正常工作。如果我以这种方式编写代码:

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

只有导航栏手势工作,子视图手势不起作用。如果我改变了这段代码的顺序。导航栏手势不起作用,但子视图手势工作。如何向两个视图添加手势?