Created
April 27, 2021 16:00
-
-
Save ryanashcraft/59c10acf945dd7dc7b8a5af6766e3d0e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// 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)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Submitted feedback to Apple with hopes that this gets fixed in a future SwiftUI update. FB9090392