2017-10-17 35 views
0

我有兩個viewControllers,都是一個UINavigationController的一部分。 從View1我可以點擊一個UITableViewCell,去View2,從View2我可以刷卡回View1獲取的UINavigationController的進步刷回

我想知道我怎麼能得到這個過渡的進展,我一直不成功以下:

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

going功能被稱爲過渡期間很多,但打印語句只是打印nil。我試圖與其他視圖控制器(View1和父導航控制器)無濟於事。

在此先感謝

+1

也許navigationController? transitionCoordinator ... –

+0

,打印可選的(0.0),在開始,然後零後 – Will

回答

3

這是什麼似乎工作:

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) 
     } 

    } 

} 

你應該能夠看到百分比打印出來,你拖動手指,駁回目前推視圖控制器。

+0

謝謝,但是進度永遠不會被我印 – Will

+0

@Will確保您添加正確的方法手勢識別。我改了名字。 –

+0

@請檢查我的編輯以生成測試場景。 –

相關問題