代码之家  ›  专栏  ›  技术社区  ›  Paul T.

uiviewController解除问题

  •  0
  • Paul T.  · 技术社区  · 6 年前

    我想等到disclose动画完成,但我不想在代码中使用很多块,所以我在 UIViewController extension (就像几年前我做的那样):

    func dismissAnimated() {
       var comleted: Bool = false
       self.dismiss(animated: true) {
          comleted = true
       }
    
       while !comleted {
          RunLoop.current.run(mode: RunLoop.Mode.common, before: Date.distantFuture)
       }
    }
    

    所以现在不是:

    viewController.dismiss(animated: true) {
        // code after completion
    }
    

    我应该写:

    viewController.dismissAnimated()
    // code after completion
    

    但它不会解除视图控制器,也不会进入完成块。

    我尝试了不同的runloop模式,尝试了不同的日期,尝试了插入runloop.current.run-into while条件,但它不起作用。有什么办法可以做到吗?

    编辑:

    它可以在iOS 9或类似的版本上运行(可能会有一些代码更改,因为我找不到源代码)。我开始 RunLoop.current.run 以避免阻塞主线程。例如,如果我 completed = true 在dispatchque.main.asyncAfter中,它将工作,问题是 dismiss

    3 回复  |  直到 6 年前
        1
  •  1
  •   Goergisn    6 年前

    我再试了一次,因为我很好奇,这个解决方案实际上对我有效:

    @objc private func dismissTapped() {
    
        let dismissalTime = dismissAnimated()
        print("Dismissal took: %ld", abs(dismissalTime))
    
    }
    
    private func dismissAnimated() -> TimeInterval {
    
        let startDate = Date()
    
        var completed = false
        self.dismiss(animated: true) {
            completed = true
        }
    
        while !completed {
            RunLoop.current.run(mode: .default, before: .distantFuture)
        }
    
        return startDate.timeIntervalSinceNow
    
    }
    

    iOS 12.1.2 Swift 4.2

        2
  •  0
  •   adamfowlerphoto    6 年前

    它不会因为你的 while !completed 循环已停止主线程,并且在主线程上进行UI更新。运行任何需要在 dismiss 完成关闭?

    self.dismiss(animated: true) {
        runSomeCode()
    }
    
        3
  •  0
  •   Goergisn    6 年前

    如果它真的只是不使用块,也许这是一个解决方案?

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
    
        if self.navigationController?.isBeingDismissed ?? self.isBeingDismissed  {
            print("Dismissal completed...")
        }
    }