iOS ) SwiftUI - Scenes 정리

영모·2022년 3월 16일
0
post-thumbnail

공식문서를 정리 했습니다.

🎯 Scenes

📒 Scenes 정리

공식문서

Declare the user interface groupings that make up the parts of your app.
번역 ) 앱의 일부를 구성하는 사용자 인터페이스 그룹을 선언합니다.

  • 시스템에서 관리하는 Life Cycle이 존재한다.
  • 다양한 방식으로 보여준다. (한 번에 둘이상의 Scene도 가능)
  • View와 비슷하게 수정이 가능하다.

🎯 Life Cycle

📒 Scene 정리

공식문서

A part of an app’s user interface with a life cycle managed by the system.
번역 ) 시스템에서 관리하는 수명 주기가 있는 앱 사용자 인터페이스의 일부입니다.

protocol Scene

📒 ScenePhase 정리

공식문서

An indication of a scene’s operational state.
번역 ) Scene의 조작 상태 표시

enum ScenePhase
@Environment(\.scenePhase) private var scenePhase

단계가 변할 때마다 동작을 실행시킬 수 있으며, Environment의 ScenePhase 값을 관찰하여 현재 단계를 읽습니다.

🍺 App에서 사용하는 방법 입니다.

@main
struct MyApp: App {
    @Environment(\.scenePhase) private var scenePhase
    
    var body: some Scene {
        WindowGroup {
            MyRootView()
        }
        .onChange(of: scenePhase) { phase in
            if phase == .background {
                // Perform cleanup when all scenes within
                // MyApp go to the background.
            }
        }
    }
}

🍺 Scene에서 사용하는 방법 입니다.

struct MyScene: Scene {
    @Environment(\.scenePhase) private var scenePhase

    var body: some Scene {
        WindowGroup {
            MyRootView()
        }
        .onChange(of: scenePhase) { phase in
            if phase == .background {
                // Perform cleanup when all scenes within
                // MyScene go to the background.
            }
        }
    }
}

🍺 View에서 사용하는 방법 입니다.

struct MyView: View {
    @ObservedObject var model: DataModel
    @Environment(\.scenePhase) private var scenePhase

    var body: some View {
        TimerView()
            .onChange(of: scenePhase) { phase in
                model.isTimerRunning = (phase == .active)
            }
    }
}

🍺 3가지 Scene Phases가 있습니다.

  • active: 포그라운드에 있다.
  • inactive: 포그라운데 있지만, 동작이 멈췄다.
  • background: UI상에 보이지 않는다.

📒 SceneBuilder 정리

A result builder for composing a collection of scenes into a single composite scene.
번역 ) Scenes 구성들을 하나의 단일 씬으로 바꾸는 result builder


🎯 Primary Scenes

📒 WindowGroup 정리

A scene that presents a group of identically structured windows.
번역 ) 동일한 구조의 Windows를 보여주는 Scene

@main
struct Mail: App {
    var body: some Scene {
        WindowGroup {
            MailViewer() // Declare a view hierarchy here.
        }
    }
}

🍺 그룹에서 생성된 모든 Window는 독립적인 상태를 유지합니다.

그룹에서 생성된 각 Window는 View 계층에 의해 인스턴스화된 State 또는 StateObject 변수를 위한 새로운 저장소를 할당합니다.
WindowGroup

🍺 특정 플랫폼 별 동작을 처리합니다.

예시) macOS 에서는 다음과 같이 다수의 윈도우를 개별 Scene으로 보여주거나, 탭으로 여러 Scene을 묶어서 보여줄 수 있습니다.

🎯 Secondary Scenes

📒 Settings 정리

A scene that presents an interface for viewing and modifying an app’s settings.
번역 ) 앱의 세팅들을 보여주거나 수정하는 인터페이스를 나타내는 Scene

struct Settings<Content> where Content : View

🍺 setting scene은 오직 macOS에서 동작합니다.

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        #if os(macOS)
        Settings {
            SettingsView()
        }
        #endif
    }
}

참고 자료

애플 공식 문서

profile
iOS를 좋아하고, 제품을 만들어가는 과정과 실패를 소중하게 여깁니다.

0개의 댓글

관련 채용 정보