이전글: 2024.08.03 - [🔨 개발/💡 PROJECT.흑백논리] - 프로젝트 개요

 

Main.storyboard 파일
Main.storyboard 파일

앱을 실행했을 때, 스플래시 화면이 1.5초간 보여지고 다음 화면으로 넘어가도록 하는 기능

프로젝트 착수 이후, 아직 로고도, 완벽한 기획도 없지만 바로 개발에 뛰어들었다. 무엇보다 swift를 이용한 iOS 앱 개발이 처음이었기에, 나름의 공부도 필요하기 때문에, 기획이 다 나오고 나서 재빠르게 개발이 뒤따라갈 수 있는 구조는 아니다. 그래서 xcode라는 개발환경에 친숙해질 겸, swift가 어떻게 돌아가는지도 알아볼 겸 디자인 하나없는 빈 앱의 뼈대를 세워나가기 시작했다.

먼저, Main.storyboard 파일에 두 개의 ViewController(SplashViewController.swift, GamingViewController.swift)를 추가하고, 대략적인 로고와 내용물(?)을 던져넣었다. 가장 먼저 할 일은 앱을 실행했을 때, 스플래시 화면이 2초간 보여지고 게임 중 화면으로 트랜지션 되도록 할 것.

Main.storyboard 구성

swift에는 LaunchScreen.storyboard라는 파일이 프로젝트 생성과 동시에 만들어져 있는데, 처음엔 여기에다 로고를 넣었더니 문제가 발생했다. 앱을 설치하고 최초 실행할때에는 LaunchScreen의 화면이 길게 보여지는데, 이후에는 정말 재빠르게 사라지는 문제가 있었다. 이를 해결하고자 Main.storyboard 파일에 스플래시 화면과 게임중 화면을 둘 다 집어넣은 것. 구글링을 좀 해보니 하나의 .storyboard 파일에 하나의 viewcontroller를 사용하는 것이 앱의 성능에 유리하다고 하는데, 우리의 사이드프로젝트는 많은 화면으로 구성되지 않을 듯하여(사실 아직 내가 잘 몰라서ㅋㅋ) 하나의 .storyboard에 두 개의 viewcontroller를 연결하였다.

SplashViewController.swift

첫 번째 viewcontroller는 Splash화면을 보여주는 뷰컨트롤러로, 1.5초 시간을 머물고 다음 화면인 게임중화면(GamingViewController.swift)로 넘어가도록 만들었다.

import UIKit

class SplashViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 1.5초 후에 게임중 화면으로 전환
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
            self.transitionToGamingViewController()
        }
    }
    
    func transitionToGamingViewController() {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        if let gamingViewController = storyboard.instantiateViewController(withIdentifier: "GamingViewController") as? GamingViewController {
            gamingViewController.modalTransitionStyle = .crossDissolve
            gamingViewController.modalPresentationStyle = .fullScreen
            self.present(gamingViewController, animated: true, completion: nil)
        }
    }
}

SceneDelegate.swift

SceneDelegate.swift에서 Main.storyboard를 최초 로드해서 띄워주도록 구성

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        
        guard let windowScene = (scene as? UIWindowScene) else { return }
        
        let window = UIWindow(windowScene: windowScene)
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController = storyboard.instantiateInitialViewController()
        window.rootViewController = initialViewController
        self.window = window
        window.makeKeyAndVisible()
    }

    func sceneDidDisconnect(_ scene: UIScene) {
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
    }

    func sceneWillResignActive(_ scene: UIScene) {
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
    }
}

 

이렇게 작성하고 build하면, 아주 귀엽고 작은 첫 빌드가 생성된다.

화면 전환 테스트 영상

 

728x90

'🔨 개발 > 💡 PROJECT.흑백논리' 카테고리의 다른 글

2. 헤더 메뉴 추가하기  (0) 2024.08.20
0. 프로젝트 착수  (0) 2024.08.03

+ Recent posts