我怎样才能实现“向右拖动以解雇”一个位于导航堆栈中的View Controller? [英] How can I implement "drag right to dismiss" a View Controller that's in a navigation stack?
问题描述
默认情况下,如果从屏幕的左边缘向右拖动,它将拖离ViewController并将其从堆栈中取出。
By default, if you drag right from the left edge of the screen, it will drag away the ViewController and take it off the stack.
我想要扩展这个功能到整个屏幕。当用户在任何地方向右拖动时,我希望发生同样的情况。
I want to extend this functionality to the entire screen. When the user drags right anywhere, I'd like the same to happen.
我知道我可以实现向右滑动手势并只需调用 self.navigationController?.popViewControllerAnimated(true)
I know that I can implement a swipe right gesture and simply call self.navigationController?.popViewControllerAnimated(true)
但是,没有拖动动作。我希望用户能够右键拖动视图控制器,就好像它是一个对象,揭示了底层的内容。并且,如果它被拖过50%,则将其解雇。 (查看Instagram以了解我的意思。)
However, there is no "dragging" motion. I want the user to be able to right-drag the view controller as if it's an object, revealing what's underneath. And, if it's dragged past 50%, dismiss it. (Check out instagram to see what I mean.)
推荐答案
在Github制作了一个演示项目
https:// github。 com / rishi420 / SwipeRightToPopController
Made a demo project in Github
https://github.com/rishi420/SwipeRightToPopController
我用过 UIViewControllerAnimatedTransitioning
protocol
I've used UIViewControllerAnimatedTransitioning
protocol
来自doc:
//这用于百分比驱动的交互式转换,以及容器控制器......
// This is used for percent driven interactive transitions, as well as for container controllers ...
在控制器的视图中添加了 UIPanGestureRecognizer
。这是手势的动作:
Added a UIPanGestureRecognizer
to the controller's view. This is the action of the gesture:
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:
指定要执行的segue并分配UINavigationController
委托。InteractiveTransitioning
-
.Changed:
具有百分比的UpdateInteractiveTransition需要委托 -
.Ended:
如果拖动50%或更高或更高速度,则继续保持转换否则取消 -
.Cancelled,.Failed:
取消转换
- Calculate the percentage of drag on the view
.Begin:
Specify which segue to perform and assignUINavigationController
delegate. delegate will be needed forInteractiveTransitioning
.Changed:
UpdateInteractiveTransition with percentage.Ended:
Continue remaining transitioning if drag 50% or more or higher velocity else cancel.Cancelled, .Failed:
cancel transitioning
参考文献:
- UIPercentDrivenInteractiveTransition
- https://github.com/visnup/swipe-left
- https://github.com/robertmryan/ScreenEdgeGestureNavigationController
- https://github.com/groomsy/custom-navigation-animation-transition-demo
- UIPercentDrivenInteractiveTransition
- https://github.com/visnup/swipe-left
- https://github.com/robertmryan/ScreenEdgeGestureNavigationController
- https://github.com/groomsy/custom-navigation-animation-transition-demo
这篇关于我怎样才能实现“向右拖动以解雇”一个位于导航堆栈中的View Controller?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!