在 UIStoryboardSegue 中添加子视图

Adding a subview inside of a UIStoryboardSegue

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

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

如何在 source.viewdestination.view 之间插入来自 UIStoryboardSegue 子类的第二个视图?显然我不能只做 addSubview,因为没有要添加的视图。在 UIStoryboardSegue 中是否还有其他地方可以添加视图,这样我仍然可以创建此 segue?

谢谢。

如果这是你想要的

你可以很容易地做到,

第 1 步:

创建 UIStoryboardSegue 的子class 并覆盖 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()
                })
            }
        }
    }
}

虽然代码看起来又大又复杂,但实际上却非常简单。

使用

获取sourcedestinationviewController'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 的框架,使它们位于屏幕边界以下,我将设置它们的动画以出现在 UIView.animate

现在显然是因为动画发生在您应用的键 window 上访问键 window

let window = UIApplication.shared.keyWindow

现在根据需要将子视图插入 window。

    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)

就这些:)

现在打开你的故事板,从你的第一个 VC 拖一个 segue 到第二个 VC 和 select segue class as SpecialEffectSegue 就是这样现在享受

希望对您有所帮助:)