【问题标题】:Get progress of UINavigationController swipe back获取 UINavigationController 的进度向后滑动
【发布时间】:2017-10-17 10:16:09
【问题描述】:

我有两个viewControllers,它们都是UINavigationController 的一部分。 从View1 我可以点击UITableViewCell 并转到View2,从View2 我可以滑动回到View1

我想知道如何才能获得此过渡的进度,但我未能成功:

override func viewDidAppear(_ animated: Bool) {
    navigationController?.interactivePopGestureRecognizer?.addTarget(self, action: #selector(going))
}
@objc func going(){
        print(self.transitionCoordinator?.percentComplete)
}

going 函数在转换过程中被多次调用,但 print 语句只打印 nil。我尝试使用其他视图控制器(View1 和父导航控制器)无济于事。

提前致谢

【问题讨论】:

  • 也许是导航控制器?过渡协调员...
  • 在开始时打印一个 Optional(0.0),然后再打印一个 nil

标签: ios swift


【解决方案1】:

这似乎可行:

    private var currentTransitionCoordinator: UIViewControllerTransitionCoordinator?

    @objc private func onGesture(sender: UIGestureRecognizer) {
        switch sender.state {
        case .began, .changed:
            if let ct = navigationController?.transitionCoordinator {
                currentTransitionCoordinator = ct
            }
        case .cancelled, .ended:
            currentTransitionCoordinator = nil
        case .possible, .failed:
            break
        }

        if let currentTransitionCoordinator = currentTransitionCoordinator {
            print(currentTransitionCoordinator.percentComplete)
        }

    }

放手之后就没有任何进展了。我尝试将协调器保留更长时间并在计时器上打印值,但我什至崩溃了。

无论如何,我认为这就是您所需要的。

测试场景:

创建一个新项目并导航到主情节提要。添加导航控制器并将其根视图控制器设置为故事板中的ViewController(删除自动生成的根)。

然后转到 ViewController.swift 并用以下内容覆盖它:

import UIKit

class ViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        if let controller = navigationController, controller.viewControllers.count <= 1 { // Present it first time only
            view.backgroundColor = UIColor.green
            let newController = ViewController()
            newController.view.backgroundColor = UIColor.red
            navigationController?.interactivePopGestureRecognizer?.addTarget(newController, action: #selector(onGesture))
            navigationController?.pushViewController(newController, animated: true)
        }
    }

    private var currentTransitionCoordinator: UIViewControllerTransitionCoordinator?

    @objc private func onGesture(sender: UIGestureRecognizer) {
        switch sender.state {
        case .began, .changed:
            if let ct = navigationController?.transitionCoordinator {
                currentTransitionCoordinator = ct
            }
        case .cancelled, .ended:
            currentTransitionCoordinator = nil
        case .possible, .failed:
            break
        }

        if let currentTransitionCoordinator = currentTransitionCoordinator {
            print(currentTransitionCoordinator.percentComplete)
        }

    }

}

当您拖动手指,关闭当前推送的视图控制器时,您应该能够看到打印出来的百分比。

【讨论】:

  • 谢谢,但我从来没有打印过进度
  • @Will 确保您将正确的方法添加到手势识别器。我改了名字。
  • 感谢 Matic,我能够让它进行预编辑。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 2016-06-12
  • 2020-01-17
  • 2014-09-02
  • 2012-02-27
相关资源
最近更新 更多