Xcode 启动动画故事板问题

Xcode Launch Animation Storyboard Issue

我对 swift 和 xcode 非常陌生。我最近一直在尝试创建一个有趣的应用程序...

我想要实现的目标:(App Launch Logo Animation,在应用程序加载后它会带你到家 storyboard ).我最近看了一下 this video. 它让我有点困惑,因为我的主页故事板加载不正确。

我不得不猜测这是因为 Story Board HomeViewController 正在加载黑屏?我希望它加载我制作的“主页 故事板”。

我的启动设置: 我的主页设置(我希望它加载到的内容)注意到我确实更改了 class :

启动/视图控制器代码:

import UIKit

class ViewController: UIViewController {
    
    private lazy var imageView: UIImageView = {
        lazy var imageView = UIImageView(frame: CGRect(x: 0, y: 0,width:150,height:150))
        imageView.image = UIImage(named: "RobloxStudio")
        return imageView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(imageView)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        imageView.center = view.center
        DispatchQueue.main.asyncAfter(deadline: .now()+0.5, execute: {
            self.animate()
        })
    }

    private func animate(){
        UIView.animate(withDuration: 1, animations: {
            let size = self.view.frame.size.width * 2
            let diffX = size - self.view.frame.size.width
            let diffY = self.view.frame.size.height - size

            self.imageView.frame = CGRect(
                x: -(diffX/2),
                y: diffY/2,
                width: size,
                height: size
            )
        })

        UIView.animate(withDuration: 1.5, animations: {
            self.imageView.alpha = 0
        }, completion: { done in
            if done{
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
                    let viewController = HomeViewController()
                    viewController.modalTransitionStyle = .crossDissolve
                    viewController.modalPresentationStyle = .fullScreen
                    self.present(viewController, animated: true)
                })
            }
        })


    }
}

HomeViewController 代码:


import UIKit

class HomeViewController: UIViewController{
    override func viewDidLoad(){
        super.viewDidLoad()
    }
}

有没有更简单的方法来制作启动动画,并将其加载到我已有设计的情节提要中?有什么建议吗?

您首先需要在故事板中为您的 HomeViewController 添加一个故事板 ID,按照惯例,您通常使用视图控制器的名称,因此您的故事板 ID 将为 "HomeViewController"

因此在以红色突出显示的框中,输入文本 "HomeViewController":

完成后,您需要实例化它。您可以将 ViewController 中的以下代码更改为:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
    let viewController = HomeViewController()
    viewController.modalTransitionStyle = .crossDissolve
    viewController.modalPresentationStyle = .fullScreen
    self.present(viewController, animated: true)
})

为此:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
    viewController.modalTransitionStyle = .crossDissolve
    viewController.modalPresentationStyle = .fullScreen
    self.present(viewController, animated: true)
})

在这段代码中,我们首先找到名为 "Main"

UIStoryboard
let storyboard = UIStoryboard(name: "Main", bundle: nil)

然后我们寻找与我们在情节提要中设置的 identifier 相同的情节提要,在本例中为 "HomeViewController"

let viewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController

最后我们在viewController上设置modalTransitionStylemodalPresentationStyle,然后再呈现。

viewController.modalTransitionStyle = .crossDissolve
viewController.modalPresentationStyle = .fullScreen
self.present(viewController, animated: true)