這是什麼似乎工作:
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)
}
}
}
你應該能夠看到百分比打印出來,你拖動手指,駁回目前推視圖控制器。
也許navigationController? transitionCoordinator ... –
,打印可選的(0.0),在開始,然後零後 – Will