Skip to content

Instantly share code, notes, and snippets.

@fatbobman
Created November 7, 2022 07:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fatbobman/96d3ec81f4ad9742086994f5feae6268 to your computer and use it in GitHub Desktop.
Save fatbobman/96d3ec81f4ad9742086994f5feae6268 to your computer and use it in GitHub Desktop.
避免在 dismiss Sheet 后快速返回 NavigationView 上层视图导致的应用锁死
/*
当在 NavigationView 非根视图中显示 sheet 时,在 dismiss Sheet 后快速用手势( 从左向右 )返回上层视图会导致整个 UI 锁死,
应用从此无法响应。写了个 ViewModifer ,来避免这种情况
*/
public extension Notification.Name {
static let disableNavigationViewGesture = Notification.Name("disableNavigationViewGesture")
}
public struct NavigationViewGestureMaskModifier: ViewModifier {
@State private var enable = false
public func body(content: Content) -> some View {
content
.overlay(
VStack {
if enable {
Color.white.opacity(0.001)
}
}
)
.onReceive(NotificationCenter.default.publisher(for: .disableNavigationViewGesture)) { notification in
guard let enable = notification.object as? Bool else { return }
self.enable = enable
}
}
}
public extension View {
func disableGestureWhenSheetPopup() -> some View {
modifier(NavigationViewGestureMaskModifier())
}
}
public struct CallDisableGestureWhenSheetPopup {
public func callAsFunction(_ disableGesture: Bool) {
NotificationCenter.default.post(name: .disableNavigationViewGesture, object: disableGesture)
}
}
struct CallDisableGestureWhenSheetPopupKey: EnvironmentKey {
static var defaultValue = CallDisableGestureWhenSheetPopup()
}
public extension EnvironmentValues {
var disableNavigationViewGesture: CallDisableGestureWhenSheetPopup {
self[CallDisableGestureWhenSheetPopupKey.self]
}
}
struct RootView: View {
var body: some View {
NavigationStack {
NavigationLink("Hello", destination: DestinationView())
}
.disableGestureWhenSheetPopup()
}
}
struct DestinationView: View {
@State var popUp = false
@Environment(\.disableNavigationViewGesture) var disable
var body: some View {
Button("Show Sheet") {
disable(true)
popUp.toggle()
}
.sheet(isPresented: $popUp, onDismiss: {
disable(false)
}) {
Text("HI")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment