Created
September 23, 2024 09:14
-
-
Save Codelaby/ecaa5396836d3c26d5b986bca9f2bec6 to your computer and use it in GitHub Desktop.
Demo SplaschScreen
This file contains 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 SplashScreen: View { | |
var body: some View { | |
ZStack { | |
Rectangle().fill(Color.blue.gradient) | |
Image(systemName: "apple.logo") | |
.resizable() | |
.aspectRatio(contentMode: .fit) | |
.frame(width: 72) | |
} | |
.ignoresSafeArea() | |
} | |
} | |
struct RootView: View { | |
var body: some View { | |
ZStack { | |
Text("Root view") | |
} | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
.background(.yellow) | |
} | |
} | |
struct CustomSplashTransition2: Transition { | |
var isRoot: Bool | |
func body(content: Content, phase: TransitionPhase) -> some View { | |
content.rotation3DEffect( | |
.degrees(phase.isIdentity ? 0 : isRoot ? 70 : -70), | |
axis: (x: 0, y: 1, z: 0), | |
anchor: isRoot ? .leading : .trailing | |
) | |
.offset(x: phase.isIdentity ? 0 : isRoot ? screenSize.width : -screenSize.width) | |
} | |
/// Current Screen Size (Without the usage of GeometryReader) | |
var screenSize: CGSize { | |
if let screenSize = (UIApplication.shared.connectedScenes.first as? UIWindowScene)? .screen.bounds.size { | |
return screenSize | |
} | |
return .zero | |
} | |
} | |
struct ContentMain: View { | |
@State var showsSplashScreen: Bool = true | |
var body: some View { | |
ZStack { | |
if showsSplashScreen { | |
SplashScreen() | |
.transition(CustomSplashTransition2(isRoot: false)) | |
} else { | |
RootView() | |
.transition(CustomSplashTransition2(isRoot: true)) | |
} | |
} | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
.background(.black) | |
.ignoresSafeArea() | |
.task { | |
guard showsSplashScreen else { return } | |
try? await Task.sleep(for: .seconds(2.5)) | |
DispatchQueue.main.async { | |
withAnimation(.smooth(duration: 0.55)) { | |
showsSplashScreen = false | |
} | |
} | |
} | |
} | |
var safeArea: UIEdgeInsets { | |
if let safeArea = (UIApplication.shared.connectedScenes.first as? UIWindowScene)? .keyWindow?.safeAreaInsets { | |
return safeArea | |
} | |
return .zero | |
} | |
} | |
#Preview { | |
ContentMain() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment