ios - 自定义交叉淡入淡出与黑色之间的切换

标签 ios swift uikit segue tvos

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

我正在寻找的是这样的:

enter image description here

我确实进行了很多搜索并尝试了很多东西,但在让它发挥作用方面运气不佳。两个并发症使事情变得更加困难:

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

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

最佳答案

我终于找到了一个不错的方法。这是一个可以无缝执行转换的自定义 Segue 类。

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 的属性检查器中将此类设置为关联的类。

setting the custom segue class

关于ios - 自定义交叉淡入淡出与黑色之间的切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56289371/

相关文章:

iphone - 如何使 UINavigationBar 不下推 View ?

ios - UINavigationBar.prefersLargeTitles 打开时 UIRefreshControl 行为错误

ios - 如何从 UIImagePicker 推送到 UIViewController

ios - "Bad system call: 12"在带有 IPA 部署的 iOS 上使用静态框架时崩溃(不是 Xcode 运行)

ios - 如何让 UIButton 在 Swift Playground 中执行操作

ios - 在 ios 中弹出到下一个 viewController

ios - 如何在 Swift 中从 LPLinkMetadata 获取图像 URL

ios - 为什么需要 preferredMaxLayoutWidth 呢?

ios - UICollectionElementKindCell 的 registerNib 在使用 presentViewController 动画时不起作用 : false

ios - 我设置了 UIVCollectionView 的 contentSize ,希望它能够向上滚动,但它仍然没有增加滚动范围