首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在UIStoryboardSegue中添加子视图

在UIStoryboardSegue中添加子视图
EN

Stack Overflow用户
提问于 2018-03-20 17:11:09
回答 1查看 87关注 0票数 0

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

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

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

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-20 17:39:09

如果这是你想要的

你可以很容易地实现它,

步骤1:

创建UIStoryboardSegue的子类并重写perform

代码语言:javascript
复制
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视图

代码语言:javascript
复制
    let firstVCView = self.source.view as UIView!
    let secondVCView = self.destination.view as UIView!

因为您需要一个中间视图,这里的颜色是红色,所以您需要再创建一个视图。

代码语言:javascript
复制
    let intermediateView = UIView()
    intermediateView.backgroundColor = UIColor.red

现在获取UIScreen的宽度和高度,以便您可以配置这些视图帧,使其看起来符合您的需要。

代码语言:javascript
复制
    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块中显示出来。

很明显,因为动画发生在应用程序的键窗口上,所以访问键窗口。

代码语言:javascript
复制
let window = UIApplication.shared.keyWindow

现在根据您的需要将子视图插入到窗口。

代码语言:javascript
复制
    window?.insertSubview(intermediateView, aboveSubview: firstVCView!)
    window?.insertSubview(secondVCView!, aboveSubview: secondVCView!)

在此之后,我的视图被堆成了FirstVCView -> IntermediateView -> SecondVCView。

现在我们已经有了我们所需要的,不是吗?现在使用UIView.animate动画

代码语言:javascript
复制
   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,请说。

代码语言:javascript
复制
self.source.navigationController?.pushViewController(destination, animated: false)

(就这些:)

现在打开您的故事板,将一个segue从您的FirstVC拖到SecondVC,并选择segue类,就像现在它喜欢的SpecialEffectSegue一样。

希望它有帮助:)

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49390325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档