我有一个作为UIPageViewController的视图控制器。
使用pageviewcontroller,我可以向左、向右滑动以转到其他VCs。它起作用了!
所以,在我添加侧边栏菜单之后。添加此菜单时,我使用此代码添加手势识别器:
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()
}
}
}
最佳答案
伙计们!
因此,解决方案不是将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。接下来如何选择正确的手势识别器。默认情况下,添加新的手势识别器时,旧的识别器不起作用。当你禁用新的手势识别器,旧的开始工作!我使用代码添加了新的手势识别器:
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是布尔值,显示菜单打开和关闭的时间!
关于ios - 如何选择合适的UIGestureRecognizer?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30480350/