自定义交叉淡入淡出 segue 和黑色

Custom cross fade segue to and from black

我正在使用 UIKit 在 Swift 中为 tvOS 开发一个应用程序,我想在两个 ViewController 之间实现黑色交叉淡入淡出 segue,代表整个功能并且不需要添加到源和目标 ViewController 的任何自定义视图或代码。

我要找的是这样的:

我确实进行了很多搜索并尝试了很多方法,但都没有成功。两个并发症使它变得更加困难:

  1. 我的一个 ViewControllers 包含在一个 UISearchController 中。
  2. 我的背景不是黑色的。

如果能得到任何帮助,我将不胜感激。

我终于找到了一个不错的方法。这是一个自定义的 segue class,它可以无缝地执行转换。

StoryboardFadeToBlackSegue.swift

import UIKit
import Foundation

class StoryboardFadeToBlackSegue: UIStoryboardSegue {

  override func perform() {
    guard let window = (UIApplication.shared.delegate as? AppDelegate)?.window else {
      super.perform()
      return
    }
    let overlay = UIView(frame: window.frame)
    overlay.layer.zPosition = 1
    overlay.backgroundColor = .black
    overlay.alpha = 0.0
    window.addSubview(overlay)
    UIView.animate(withDuration: 0.5, delay: 0, options: [.curveEaseOut], animations: {
      overlay.alpha = 1.0
    }, completion: { _ in
      self.source.present(self.destination, animated: false, completion: {
        UIView.animate(withDuration: 0.6, delay: 0, options: [.curveEaseIn], animations: {
          overlay.alpha = 0.0
        }, completion: { _ in
          overlay.removeFromSuperview()
        })
      })
    })
  }
}

为了使用它,请在您的 Segue 的属性检查器中将此 class 设置为关联的 Class。