Splash Screen | Custom Launch Screen in Swift | Launch Screen Animation in App (Swift 5.3) iOS Development




In this article, we will learn how to add a launch screen iphone app screen animation to your app in Swift 5 and Xcode 11. In this way, when the app launches the user will get the impression that the launch screen is animating. It is similar to the twitter app launch animation and other popular apps.

Let’s start first create new project, open to the Assets.xcassets and add the logo image.


Open the existing LaunchScreen.storyboard your project may already have and add image 
look like this.


Open the ViewController.swift class and call the func animate().

The view controller implementation will look like this: 


//
//  ViewController.swift
//  Helo
//
//  Created by Rathaur on 02/07/20.
//  Copyright © 2020 AppCodeZip. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    private let imageView: UIImageView = {
        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
        imageView.image = UIImage.init(named: "logo")
        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.width * 3
            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)
            self.imageView.alpha = 0
        })
        
        DispatchQueue.main.asyncAfter(deadline: .now()+0.6, execute: {
            let home = HomeScreen()
            home.modalTransitionStyle = .crossDissolve
            home.modalPresentationStyle = .fullScreen
            self.present(home, animated: true)
        })
        
        
    }
}
     

After implementing the ViewController, we have to add a new file name HomeScreen.

HomeScreen will look like this:


//
//  HomeScreen.swift
//  Helo
//
//  Created by Rathaur on 09/12/20.
//  Copyright © 2020 AppCodeZip. All rights reserved.
//

import UIKit

class HomeScreen: UIViewController {
    
    let lebel: UILabel = {
       let lebel = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 50))
   
           lebel.textAlignment = .center
           lebel.font = .systemFont(ofSize: 25, weight: .bold)
           lebel.text = "HomeScreen!"
           return lebel
       }()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(lebel)
        lebel.center = view.center
        view.backgroundColor = .brown
    }
}

Here's the final splash screen effect, the app is built and then run.




HAPPY LEARNING! 


Similar Xcode tutorial, swift tutorial solutions you may also like...

Post a Comment

0 Comments