获取UINavigationController向后滑动的进度 [英] Get progress of UINavigationController swipe back
问题描述
我有两个 viewControllers
,它们都是 UINavigationController
的一部分.在 View1
中,我可以点击 UITableViewCell
并转到 View2
,在 View2
中,我可以滑动回View1
.
I have two viewControllers
, both are part of a UINavigationController
.
From View1
I can tap a UITableViewCell
and go to View2
, from View2
I can swipe back to View1
.
我想知道如何获得过渡的进展,但是我在以下方面没有成功:
I would like to know how I can get the progress of this transition, and I have been unsuccessful with the following:
override func viewDidAppear(_ animated: Bool) {
navigationController?.interactivePopGestureRecognizer?.addTarget(self, action: #selector(going))
}
@objc func going(){
print(self.transitionCoordinator?.percentComplete)
}
该 going
函数在过渡期间被调用很多,但是该print语句仅打印 nil
.我尝试使用其他视图控制器(View1和父级导航控制器)都无济于事.
That going
function is called a lot during the transition, but that print statement just prints nil
. I tried with other view controller (both View1 and the parent nav controller) to no avail.
预先感谢
推荐答案
这似乎可行:
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)
}
}
放开之后,就无法获得进步.我尝试将协调器保留更长的时间,并在计时器上打印值,但我甚至崩溃了.
After you let go there is no way to get progress though. I tried preserving the coordinator for a bit longer and printing values on timer but I get a crash even.
无论如何,我认为这就是您所需要的.
Anyway, I assume this is what you need.
测试场景:
创建一个新项目,然后导航到主故事板.添加一个导航控制器,并将其根视图控制器设置为情节提要中的 ViewController
(删除自动生成的根).
Create a new project and navigate to main storyboard. Add a navigation controller and set its root view controller to ViewController
in storyboard (remove the autogenerated root).
然后转到ViewController.swift并使用以下代码覆盖它:
Then go to ViewController.swift and overwrite it with following:
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)
}
}
}
您应该能够看到在拖动手指时打印出的百分比,从而消除了当前按下的视图控制器.
You should be able to see percentage printed out as you drag your finger, dismissing currently pushed view controller.
这篇关于获取UINavigationController向后滑动的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!