在UIStoryboardSegue中添加子视图

时间:2018-03-20 17:11:10

标签: ios swift uistoryboardsegue

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

enter image description here

现在,segue背后的基本思想很简单,向上滑动source.view,然后在最终滑动destination.view之前向上滑动一个视图。但是,我的问题如下:

如何在source.view的子类的destination.viewUIStoryboardSegue之间插入第二个视图?显然我不能只做addSubview,因为没有要添加的视图。还有另一个地方可以在UIStoryboardSegue中添加一个视图,所以我仍然可以创建这个segue吗?

感谢。

1 个答案:

答案 0 :(得分:4)

如果这是你想要的

enter image description here

你可以很容易地实现它,

第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()
                })
            }
        }
    }
}

尽管代码看起来庞大而复杂但其中发生的事情非常简单。

使用

获取sourcedestination 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)

请注意,我设置了SecondVCintermediateView的框架,使得它们位于屏幕边界之下,并且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,它现在享受

enter image description here

希望有所帮助:)