Skip to content

Instantly share code, notes, and snippets.

@ryanashcraft
Created April 27, 2021 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanashcraft/59c10acf945dd7dc7b8a5af6766e3d0e to your computer and use it in GitHub Desktop.
Save ryanashcraft/59c10acf945dd7dc7b8a5af6766e3d0e to your computer and use it in GitHub Desktop.
import SwiftUI
// Works around a critical issue observed on iPads with a trackpad cursor.
//
// With highPriorityGesture, other interactive content on the screen can become unresponsive after
// triggering the gesture with an iPad cursor. It's as if the gesture seems to never relinquish its
// grasp on the entire UI, until you interact with the screen directly with a tap or trigger a
// context menu.
//
// This view modifier and extension works around this by forcing the gesture to be "cancelled" by
// making it temporarily disabled through a .none gesture mask, then toggling it back to its default
// mask after an instant.
public struct PatchedHighPriorityGesture<G: Gesture>: ViewModifier {
private let gesture: G
private let mask: GestureMask
@State private var disableGesture = false
public init(gesture: G, including mask: GestureMask) {
self.gesture = gesture
self.mask = mask
}
private var patchedGesture: some Gesture {
gesture.onEnded { _ in
self.disableGesture = true
DispatchQueue.main.asyncAfter(deadline: .now() + TimeInterval(0.5)) {
self.disableGesture = false
}
}
}
public func body(content: Content) -> some View {
content
.highPriorityGesture(patchedGesture, including: disableGesture ? .none : mask)
}
}
public extension View {
func patchedHighPriorityGesture<G: Gesture>(gesture: G, including mask: GestureMask) -> some View {
modifier(PatchedHighPriorityGesture(gesture: gesture, including: mask))
}
}
@ryanashcraft
Copy link
Author

Submitted feedback to Apple with hopes that this gets fixed in a future SwiftUI update. FB9090392

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment