gpt4 book ai didi

ios - 在 UIStoryboardSegue 中添加 subview

转载 作者:行者123 更新时间:2023-11-28 10:09:51 25 4
gpt4 key购买 nike

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

enter image description here

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

如何在 UIStoryboardSegue 的子类中的 source.viewdestination.view 之间插入第二个 View ?显然我不能只执行 addSubview,因为没有要添加的 View 。是否有另一个地方可以在 UIStoryboardSegue 中添加 View ,这样我仍然可以创建这个 segue?

谢谢。

最佳答案

如果这是你想要的

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

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

获取目标viewController的 View 使用

    let firstVCView = self.source.view as UIView!
let secondVCView = self.destination.view as UIView!

因为你需要一个红色的中间 View ,所以你再创建一个 View

    let intermediateView = UIView()
intermediateView.backgroundColor = UIColor.red

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

    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< 中出现 block

现在显然是因为动画发生在应用程序的关键窗口上访问关键窗口

let window = UIApplication.shared.keyWindow

现在根据需要将 subview 插入窗口。

    window?.insertSubview(intermediateView, aboveSubview: firstVCView!)
window?.insertSubview(secondVCView!, aboveSubview: secondVCView!)

所以在此之后我将 View 堆叠为 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()
})
}
}

需要注意的重要事项是完成 block 中的 intermediateView.removeFromSuperview()

现在我决定使用 self.source.present( 如果你需要用你时髦的动画推送目标 VC 说

self.source.navigationController?.pushViewController(destination, animated: false)

就是这样:)

现在打开你的 Storyboard,将一个 segue 从你的 FirstVC 拖到 SecondVC 并选择 segue 类作为 SpecialEffectSegue 就这样吧

enter image description here

希望对您有所帮助:)

关于ios - 在 UIStoryboardSegue 中添加 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49390325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com