Skip to content

Instantly share code, notes, and snippets.

@markbattistella
Last active December 30, 2022 11:13
Show Gist options
  • Save markbattistella/e4eab2ca7bd9b2b27fcbb8e2ad616c08 to your computer and use it in GitHub Desktop.
Save markbattistella/e4eab2ca7bd9b2b27fcbb8e2ad616c08 to your computer and use it in GitHub Desktop.
Hide the details of a View when resigning a SwiftUI app to background
struct MaskingViewModifier: ViewModifier {
@Environment(\.scenePhase)
private var scenePhase
@State private var selectedPhase: ScenePhase?
func body(content: Content) -> some View {
ZStack {
content
.blur(radius: selectedPhase == .active ? 0 : 5)
Image("splash")
.opacity(selectedPhase == .active ? 0 : 1)
}
.onChange(of: scenePhase) { phase in
switch phase {
case .background:
selectedPhase = .background
case .inactive:
selectedPhase = .inactive
case .active:
selectedPhase = .active
@unknown default:
selectedPhase = nil
}
}
}
}
// add it to the view for easy access
extension View {
func maskView() -> some View {
return modifier(MaskingViewModifier())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment