默认情况下,如果从屏幕的左边缘向右拖动,它将拖动ViewController并将其移出堆栈。
我想将此功能扩展到整个屏幕。当用户向右拖动时,我希望发生同样的事情。
我知道我可以执行向右滑动手势,只需调用 self.navigationController?.popViewControllerAnimated(true)
self.navigationController?.popViewControllerAnimated(true)
但是,没有“拖动”动作。我希望用户能够像拖动鼠标一样右键拖动视图控制器,以显示其下方。并且,如果将其拖到50%以上,请将其关闭。(查看instagram了解我的意思。)
在Github中制作了一个演示项目 https://github.com/rishi420/SwipeRightToPopController
我用过UIViewControllerAnimatedTransitioning协议
UIViewControllerAnimatedTransitioning
从文档中:
//这用于百分比驱动的交互式过渡以及容器控制器…
UIPanGestureRecognizer在控制器的视图中添加了一个。这是手势的动作:
UIPanGestureRecognizer
func handlePanGesture(panGesture: UIPanGestureRecognizer) { let percent = max(panGesture.translationInView(view).x, 0) / view.frame.width switch panGesture.state { case .Began: navigationController?.delegate = self navigationController?.popViewControllerAnimated(true) case .Changed: percentDrivenInteractiveTransition.updateInteractiveTransition(percent) case .Ended: let velocity = panGesture.velocityInView(view).x // Continue if drag more than 50% of screen width or velocity is higher than 1000 if percent > 0.5 || velocity > 1000 { percentDrivenInteractiveTransition.finishInteractiveTransition() } else { percentDrivenInteractiveTransition.cancelInteractiveTransition() } case .Cancelled, .Failed: percentDrivenInteractiveTransition.cancelInteractiveTransition() default: break } }
脚步:
.Begin:
UINavigationController
InteractiveTransitioning
.Changed:
.Ended:
.Cancelled, .Failed:
参考文献: