我怎样才能实现“向右拖动以解雇”一个位于导航堆栈中的View Controller? [英] How can I implement "drag right to dismiss" a View Controller that's in a navigation stack?

查看:138
本文介绍了我怎样才能实现“向右拖动以解雇”一个位于导航堆栈中的View Controller?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,如果从屏幕的左边缘向右拖动,它将拖离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
    }
}

步骤:


  1. 计算视图上拖动的百分比

  2. .Begin:指定要执行的segue并分配 UINavigationController 委托。 InteractiveTransitioning

  3. .Changed:具有百分比的UpdateInteractiveTransition需要委托

  4. .Ended:如果拖动50%或更高或更高速度,则继续保持转换否则取消

  5. .Cancelled,.Failed:取消转换

  1. Calculate the percentage of drag on the view
  2. .Begin: Specify which segue to perform and assign UINavigationController delegate. delegate will be needed for InteractiveTransitioning
  3. .Changed: UpdateInteractiveTransition with percentage
  4. .Ended: Continue remaining transitioning if drag 50% or more or higher velocity else cancel
  5. .Cancelled, .Failed: cancel transitioning


参考文献:


  1. UIPercentDrivenInteractiveTransition

  2. https://github.com/visnup/swipe-left

  3. https://github.com/robertmryan/ScreenEdgeGestureNavigationController

  4. https://github.com/groomsy/custom-navigation-animation-transition-demo

  1. UIPercentDrivenInteractiveTransition
  2. https://github.com/visnup/swipe-left
  3. https://github.com/robertmryan/ScreenEdgeGestureNavigationController
  4. https://github.com/groomsy/custom-navigation-animation-transition-demo

这篇关于我怎样才能实现“向右拖动以解雇”一个位于导航堆栈中的View Controller?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆