SwiftUIでアプリ起動時に画面を遷移させる
SwiftUIでアプリ起動時に画面を遷移させる方法です。

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
@State var viewType: ViewType = .launch | |
var body: some View { | |
ZStack { | |
switch viewType { | |
case .launch: | |
Text("Launch") | |
case .home: | |
Text("Home") | |
} | |
} | |
.onAppear { | |
Task { | |
try? await sleep() | |
withAnimation(.linear(duration: 0.5)) { | |
viewType = .home | |
} | |
} | |
} | |
} | |
func sleep() async throws { | |
// sleep 2sec | |
_ = try await Task.sleep(nanoseconds: 2_000_000_000) | |
} | |
} | |
#Preview { | |
ContentView() | |
} |