这似乎是有效的:
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)
}
}
}
当您拖动手指时,您应该能够看到打印出来的百分比,从而消除当前推送的视图控制器。