我正在编写一个应用程序,我需要使用相当“复杂”的UIStoryboardSegue
。设计师给了我以下内容:
现在,segue背后的基本思想很简单,向上滑动source.view
,然后在最终滑动destination.view
之前向上滑动一个视图。但是,我的问题如下:
如何在source.view
的子类的destination.view
和UIStoryboardSegue
之间插入第二个视图?显然我不能只做addSubview
,因为没有要添加的视图。还有另一个地方可以在UIStoryboardSegue
中添加一个视图,所以我仍然可以创建这个segue吗?
感谢。
答案 0 :(得分:4)
如果这是你想要的
你可以很容易地实现它,
第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
的框架,使得它们位于屏幕边界之下,并且Ill会将它们设置为动态显示在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
,它现在享受
希望有所帮助:)