【问题标题】:Custom cross fade segue to and from black自定义交叉淡入淡出到黑色和从黑色
【发布时间】:2019-05-24 09:11:36
【问题描述】:
我正在使用UIKit 在 Swift 中为 tvOS 开发一个应用程序,我想在两个 ViewControllers 之间实现黑色交叉淡入淡出,它代表整个功能并且不需要任何自定义视图或添加到源和目标 ViewController。
我正在寻找的是这样的:
我确实搜索了很多东西,尝试了很多东西,但没有成功。两个复杂因素使这一切变得更加困难:
- 我的一个
ViewControllers 包含在UISearchController 中。
- 我的背景不是黑色的。
如果能得到任何帮助,我将不胜感激。
【问题讨论】:
标签:
ios
swift
uikit
segue
tvos
【解决方案1】:
我终于找到了一个不错的方法。这是一个无缝执行转换的自定义 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 的属性检查器中将这个类设置为关联的类。