Home > Blockchain >  Xcode Launch Animation Storyboard Issue
Xcode Launch Animation Storyboard Issue

Time:01-07

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 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?

CodePudding user response:

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)
  •  Tags:  
  • Related