Skip to content

Instantly share code, notes, and snippets.

@jamesporter
Created March 20, 2022 19:23
Show Gist options
  • Select an option

  • Save jamesporter/cb9b100eb4b49eb0a9a42ffd3903f488 to your computer and use it in GitHub Desktop.

Select an option

Save jamesporter/cb9b100eb4b49eb0a9a42ffd3903f488 to your computer and use it in GitHub Desktop.
SwiftUI (Quick) Tap Gesture

SwiftUI Quick Tap Gesture

For games etc the tap gesture is useless as it has delay. This is slightly hacky but I suspect fine for most cases.

It would be pretty easy to include/pass the location too (another weakness of the built in tap gesture is that it doesn't give you this).

import SwiftUI
// fuck it this should be fine-ish (as on main thread)?
fileprivate var lastStartPoint: CGPoint?
extension View {
func quickTap(action: @escaping () -> Void) -> some View {
gesture(DragGesture(minimumDistance: 0).onChanged { value in
if value.startLocation == lastStartPoint {
} else {
lastStartPoint = value.startLocation
action()
}
}.onEnded { value in
lastStartPoint = nil
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment