Swift:为目标视图控制器添加句柄以进行自定义导航控制器过渡

时间:2018-12-20 16:57:53

标签: ios swift uiviewcontroller uinavigationcontroller transition

简介

我正在创建一个应用程序,该应用程序在UIViewController中嵌入RootViewController,该应用程序在UINavigationController中启动。 RootViewController里面有一个UITableView,并且由UIPanGestureRecognizer触发的自定义转换附加到屏幕右边缘的小“ handleView”(类型为UIView)上, “ SlideOutViewController” UIViewController由用户拖动到屏幕上。

自定义过渡及其UIPanGestureRecognizer可以完美运行。我的自定义过渡看起来像UIScreenEdgePanGestureRecognizer用于默认的弹出式互动,但我也可以用它来推动UIViewController。即滑出式视图控制器。

问题

  • 我不知道如何在手势/转换(推动和弹出)期间将handleView附加到“ SlideOutViewController”的前沿,以及如何始终将其锚定在“ RootViewController”中在不进行过渡时位于屏幕的右边缘。

  • 我当前的尝试使“ handleView”的移动速度比过渡速度快。理想情况下,handleView应该锚定到“ SlideOutViewController”。

问题

我应该如何将其“ handleView”管理为 1。。它始终出现在RootViewController的右侧以及 2。上。自定义过渡(按入和弹出)过程中“ SlideOutViewController”的前沿?

我的代码

  1. RootViewController

    class RootViewController: UIViewController {
    
        ...
    
        let navigationDelegate = CNNavigationDelegate()
        let handleView = UIView()
    
         override func viewDidLoad() {
             super.viewDidLoad()
             navigationController?.delegate = navigationDelegate
             setupHandleView()
             navigationDelegate.addPushInteractionController(to: handleView)
             ... // Additional view controller setup
         }
    
         override func viewDidAppear(_ animated: Bool) {
             super.viewDidAppear(animated)
    
             navigationDelegate.pushDestination = {
                [weak self] in
                self?.storyboard?.instantiateViewController(withIdentifier: "CNSlideOutCalendarControllerID")
             }
         }
    
         private func setupHandleView() {
             handleView.layer.borderWidth = 1
             handleView.layer.borderColor = UIColor.black.cgColor
             handleView.layer.cornerRadius = 20
             handleView.translatesAutoresizingMaskIntoConstraints = false
             view.addSubview(handleView)
             handleView.frame = CGRect(x: view.frame.width - 12.5, y: view.frame.height / 2, width: 25, height: 60)
         }
    
    }
    
  2. SlideOutViewController

    class SlideOutViewController: UIViewController {
    
        var navigationDelegate : CNNavigationDelegate  { return navigationController?.delegate as! CNNavigationDelegate}
    
        override func viewDidLoad() {
            super.viewDidLoad()
            navigationDelegate.addPopInteractionController(to: view)
            view.backgroundColor = .red
            setupNavigationBar()
        }
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            navigationDelegate.pushDestination = nil
        }
    }
    
  3. NavigationController委托

    class CNNavigationDelegate: NSObject, UINavigationControllerDelegate {
    
        var interactionController: UIPercentDrivenInteractiveTransition?
        var current: UIViewController?
        var pushDestination: (() -> UIViewController?)?
    
        func navigationController(_ navigationController: UINavigationController,
                          animationControllerFor operation: UINavigationController.Operation,
                          from fromVC: UIViewController,
                          to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
            return CNNavigationAnimator(transitionType: operation)
        }
    
        func navigationController(_ navigationController: UINavigationController,
                          interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
            return interactionController
        }
    
        func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
            current = viewController
        }
    }
    
    // MARK: - Push
    
    extension CNNavigationDelegate {
        func addPushInteractionController(to view: UIView) {
            let swipe = UIPanGestureRecognizer(target: self, action: #selector(handlePushGesture(_:)))
            view.addGestureRecognizer(swipe)
        }
    
        @objc private func handlePushGesture(_ gesture: UIScreenEdgePanGestureRecognizer) {
            guard let pushDestination = pushDestination else { return }
    
            let position = gesture.translation(in: gesture.view?.superview)
            let percentComplete = min(-position.x / UIScreen.main.bounds.width, 1.0)
            let handleView = gesture.view!
    
            switch gesture.state {
            case .began:
                interactionController = UIPercentDrivenInteractiveTransition()
                guard let controller = pushDestination() else { fatalError("No push destination") }
                current?.navigationController?.pushViewController(controller, animated: true)
    
            case .changed:
                interactionController?.update(percentComplete)
                handleView.center = CGPoint(x: handleView.center.x + position.x, y: handleView.center.y)
                gesture.setTranslation(.zero, in: gesture.view?.superview)
            case .ended, .cancelled:
                let speed = gesture.velocity(in: gesture.view)
                if speed.x < 0 || (speed.x == 0 && percentComplete > 0.5) {
                    interactionController?.finish()
                    UIView.animate(withDuration: 0.1, delay: 0.5, options: .curveLinear, animations: {
                        handleView.center = CGPoint(x: UIScreen.main.bounds.width, y: handleView.center.y)
                    })
                } else {
                    interactionController?.cancel()
                    UIView.animate(withDuration: 0.5, delay: 0, options: .curveLinear, animations: {
                        handleView.center = CGPoint(x: UIScreen.main.bounds.width, y: handleView.center.y)
                    })
                }
                interactionController = nil
    
            default:
                break
            }
        }
    }
    
    // MARK: - Pop
    
    extension CNNavigationDelegate {
        func addPopInteractionController(to view: UIView) {
            let swipe = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(handlePopGesture(_:)))
            swipe.edges = [.left]
            view.addGestureRecognizer(swipe)
        }
    
        @objc private func handlePopGesture(_ gesture: UIScreenEdgePanGestureRecognizer) {
            let position = gesture.translation(in: gesture.view)
            let percentComplete = min(position.x / gesture.view!.bounds.width, 1)
    
            switch gesture.state {
            case .began:
                interactionController = UIPercentDrivenInteractiveTransition()
                current?.navigationController?.popViewController(animated: true)
    
            case .changed:
                interactionController?.update(percentComplete)
    
            case .ended, .cancelled:
                let speed = gesture.velocity(in: gesture.view)
                if speed.x > 0 || (speed.x == 0 && percentComplete > 0.5) {
                    interactionController?.finish()
                } else {
                    interactionController?.cancel()
                }
                interactionController = nil
    
    
            default:
                break
            }
        }
    }
    
  4. NavigationAnimator

    class CNNavigationAnimator: NSObject, UIViewControllerAnimatedTransitioning {
    
        let transitionType: UINavigationController.Operation
    
        init(transitionType: UINavigationController.Operation) {
            self.transitionType = transitionType
            super.init()
        }
    
        func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
            let inView   = transitionContext.containerView
            let toView   = transitionContext.view(forKey: .to)!
            let fromView = transitionContext.view(forKey: .from)!
    
            var frame = inView.bounds
    
            switch transitionType {
            case .push:
                frame.origin.x = frame.size.width
                toView.frame = frame
    
                inView.addSubview(toView)
                UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                    toView.frame = inView.bounds
                }, completion: { finished in
                    transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
                })
    
            case .pop:
                toView.frame = frame
                inView.insertSubview(toView, belowSubview: fromView)
    
                UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                    frame.origin.x = frame.size.width
                    fromView.frame = frame
                }, completion: { finished in
                    transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
                })
    
            case .none:
                break
            }
        }
    
        func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
            return 0.5
        }
    }
    

感谢您阅读我的问题。

0 个答案:

没有答案