如何选择合适的UIGestureRecognizer?

时间:2015-05-27 10:51:17

标签: ios swift uigesturerecognizer

我有视图控制器,里面有UIPageViewController。

使用pageviewcontroller我可以向左,向右转,以便转到其他VC。有用!

所以,在我添加了sideBarMenu之后。添加此菜单时,我使用此代码添加手势识别器:

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

    }

这里的sourceViewController是我原来的VC。

问题是当我尝试滑动(为了关闭菜单)时,pageViewController滑动工作。

我想在菜单打开时禁用pageViewController滑动并启用新的滑动功能。当菜单关闭时,反之亦然。

附加代码:

 func handleOffstagePan(pan: UIPanGestureRecognizer){
    println("dismiss pan gesture recognizer")

    let translation = pan.translationInView(pan.view!)

    let d = translation.x / CGRectGetWidth(pan.view!.bounds) * -0.5

    switch (pan.state) {

    case UIGestureRecognizerState.Began:
        self.interactive = true

        self.menuViewController.performSegueWithIdentifier("dismisMenu", sender: self)

        break
    case UIGestureRecognizerState.Changed:
        self.updateInteractiveTransition(d)
        break
    default:
        self.interactive = false

        if d > 0.1 {

            self.finishInteractiveTransition()
        }else {

            isMenuVisible = false
            self.cancelInteractiveTransition()
        }
    }

}

1 个答案:

答案 0 :(得分:0)

专家! 所以,解决方案是将PageViewController设置为TransitionManager的sourceVC,而不是将pageContentViewController设置为sourceVC。 PageContentViewControler是:

 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)

}

这里我将pageContentVC设置为transitionManageClass的sourveVS。 NExt如何选择合适的GestureRecognizer。默认情况下,当您添加新的手势识别器时,旧的手势识别器不起作用。当您禁用新的手势识别器时,旧的手势识别器开始工作!我使用代码添加了新的手势识别器:

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

    }
}

在设置menuViewController之前,我设置了sourceViewController。所以,在这里我要为我的sourceViewController添加新的手势识别器。接下来,步骤是禁用此手势识别。当您关闭菜单时,请使用以下代码将其禁用:

 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
        }
    }
}

PresentingP是布尔值,显示菜单何时打开和关闭!