0

I'm very very new to swift, and xcode. I've recently have been trying to create a app for fun...

What I am trying to achieve : (App Launch Logo Animation that after app is loaded it will take you to the home storyboard). I recently took a look at this video. It sort of confused me because my home story board is not loading correctly.

I would have to guess this is happening because the Story Board, HomeViewController is just loading a black screen? I want it to be loading my "Home Storyboard" that I made.

My Launch Setup : enter image description here My Home Setup (what I want it to load into) notice I did change the class : enter image description here

Code For Launch / View Controller :

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


    }
}

Code For HomeViewController :


import UIKit

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

Is there any simpler way to make a launch animation, and to make it load into my story board that already has the designs? Any tips?

  • You need to load your HomeViewController from the storyboard, this should point you in the right direction https://stackoverflow.com/a/32274363/5508175 – Andrew Jan 06 '22 at 17:03
  • @Andrew looking at the post, where would the following code go for the solution answer? Still quite confused. – Constantine Linardakis Jan 06 '22 at 17:12

1 Answers1

1

You will first need to add a storyboard id to your HomeViewController in the storyboard, by convention you usually use the name of the view controller, so your storyboard id would be "HomeViewController"

So in the box highlighted in red, enter the text "HomeViewController":

enter image description here

Once you have done that then you need to instantiate it. You would change the following code in your ViewController from this:

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

To this:

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

In this code we first find the UIStoryboard called "Main"

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

We then look for the storyboard that has the same identifier that we set in the storyboard, in this case "HomeViewController"

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

Then finally we set the modalTransitionStyle and modalPresentationStyle on the viewController, before we present it.

viewController.modalTransitionStyle = .crossDissolve
viewController.modalPresentationStyle = .fullScreen
self.present(viewController, animated: true)
Andrew
  • 26,706
  • 9
  • 85
  • 101
  • Very well written, thank you very much. – Constantine Linardakis Jan 06 '22 at 21:35
  • Yet this did get to fix the viewing board problem. It seems to not do the launch animation if the class Is "HomeViewController" instead of "ViewController". What could be the problem there? – Constantine Linardakis Jan 06 '22 at 21:43
  • You need two view controllers in your Main storyboard. One that uses ViewController and is your initial view controller for the storyboard, and another that is your HomeViewController. I suggest you watch the tutorial again and follow it exactly. Once you are comfortable with it, try editing it to do what you want. – Andrew Jan 06 '22 at 22:43
  • Alright thank you the information. Just checking the "Main" storyboard though would have class ```HomeViewController``` and so for the storyboard id correct? What happens is it, does the animation but then breaks/errors. Says "Could not cast value of type 'Roblox_Learning.ViewController'" – Constantine Linardakis Jan 07 '22 at 03:31
  • There should only be two view controllers inside your storyboard file. One called `ViewController` which is the initial view controller in the storyboard where the animation starts from. And one called `HomeViewController` where it ends. Both of these view controllers should be in the Main storyboard. Both should have identifiers that match their class name. – Andrew Jan 07 '22 at 10:30