我正在编写一个应用程序,我需要使用一个相当“复杂”的UIStoryboardSegue。设计师给了我以下内容:

现在,segue背后的基本思想是简单的,向上滑动source.view,然后再滑动一个视图,然后最终滑动destination.view。不过,我的问题如下:
如何在source.view和destination.view之间插入来自UIStoryboardSegue子类的第二个视图?显然,我不能只做addSubview,因为没有可以添加的视图。是否还有其他地方可以在UIStoryboardSegue中添加视图,以便我仍然可以创建这个segue?
谢谢。
发布于 2018-03-20 17:39:09
如果这是你想要的

你可以很容易地实现它,
步骤1:
创建UIStoryboardSegue的子类并重写perform
import UIKit
class SpecialEffectSegue: UIStoryboardSegue {
override func perform() {
let firstVCView = self.source.view as UIView!
let secondVCView = self.destination.view as UIView!
let intermediateView = UIView()
intermediateView.backgroundColor = UIColor.red
// Get the screen width and height.
let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height
// Specify the initial position of the destination view.
secondVCView?.frame = CGRect(x: 0.0, y: screenHeight, width: screenWidth, height: screenHeight)
intermediateView.frame = CGRect(x: 0.0, y: screenHeight, width: screenWidth, height: screenHeight)
// Access the app's key window and insert the destination view above the current (source) one.
let window = UIApplication.shared.keyWindow
window?.insertSubview(intermediateView, aboveSubview: firstVCView!)
window?.insertSubview(secondVCView!, aboveSubview: secondVCView!)
UIView.animate(withDuration: 0.4, animations: { () -> Void in
firstVCView?.frame = ((firstVCView?.frame)?.offsetBy(dx: 0.0, dy: -screenHeight))!
intermediateView.frame = (intermediateView.frame.offsetBy(dx: 0.0, dy: -screenHeight))
}) { (Finished) -> Void in
UIView.animate(withDuration: 0.4, animations: { () -> Void in
secondVCView?.frame = (secondVCView?.frame.offsetBy(dx: 0.0, dy: -screenHeight))!
}) { (Finished) -> Void in
self.source.present(self.destination, animated: false, completion: {
intermediateView.removeFromSuperview()
})
}
}
}
}虽然代码看起来庞大而复杂,但在代码中发生的事情非常简单。
使用以下方法获取source和destination viewController's视图
let firstVCView = self.source.view as UIView!
let secondVCView = self.destination.view as UIView!因为您需要一个中间视图,这里的颜色是红色,所以您需要再创建一个视图。
let intermediateView = UIView()
intermediateView.backgroundColor = UIColor.red现在获取UIScreen的宽度和高度,以便您可以配置这些视图帧,使其看起来符合您的需要。
let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height
// Specify the initial position of the destination view.
secondVCView?.frame = CGRect(x: 0.0, y: screenHeight, width: screenWidth, height: screenHeight)
intermediateView.frame = CGRect(x: 0.0, y: screenHeight, width: screenWidth, height: screenHeight)请注意,我设置了SecondVC和intermediateView的框架,使它们处于屏幕边界以下,并使它们在UIView.animate块中显示出来。
很明显,因为动画发生在应用程序的键窗口上,所以访问键窗口。
let window = UIApplication.shared.keyWindow现在根据您的需要将子视图插入到窗口。
window?.insertSubview(intermediateView, aboveSubview: firstVCView!)
window?.insertSubview(secondVCView!, aboveSubview: secondVCView!)在此之后,我的视图被堆成了FirstVCView -> IntermediateView -> SecondVCView。
现在我们已经有了我们所需要的,不是吗?现在使用UIView.animate动画
UIView.animate(withDuration: 0.4, animations: { () -> Void in
firstVCView?.frame = ((firstVCView?.frame)?.offsetBy(dx: 0.0, dy: -screenHeight))!
intermediateView.frame = (intermediateView.frame.offsetBy(dx: 0.0, dy: -screenHeight))
}) { (Finished) -> Void in
UIView.animate(withDuration: 0.4, animations: { () -> Void in
secondVCView?.frame = (secondVCView?.frame.offsetBy(dx: 0.0, dy: -screenHeight))!
}) { (Finished) -> Void in
self.source.present(self.destination, animated: false, completion: {
intermediateView.removeFromSuperview()
})
}
}重要的是要注意的是intermediateView.removeFromSuperview()在完成块。
现在,我决定使用self.source.present(来显示目的地,如果您需要用时髦的动画推送目的地VC,请说。
self.source.navigationController?.pushViewController(destination, animated: false)(就这些:)
现在打开您的故事板,将一个segue从您的FirstVC拖到SecondVC,并选择segue类,就像现在它喜欢的SpecialEffectSegue一样。

希望它有帮助:)
https://stackoverflow.com/questions/49390325
复制相似问题