Last active
December 30, 2022 11:13
-
-
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
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
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