I would like to recognize to a tab gesture in SwiftUI with two fingers.
How do I change the code
var body: some View { Image("tap-image") .gesture( TapGesture() .onEnded({ print("Tapped!") }) ) }
if I only want to react to the gesture with two fingers?
Thanks,
Harald
Thanks Claude,
I hope the next version of SwiftUI will handle this better.
This is my code:
struct ContentView: View { var body: some View { Rectangle() .fill(Color.black) .frame(width: 200, height: 200) .overlay( TappableView { gesture in print("2 touches detected") }) } }
import SwiftUI struct TappableView: UIViewRepresentable { var tapCallback: (UITapGestureRecognizer) -> Void typealias UIViewType = UIView func makeCoordinator() -> TappableView.Coordinator { Coordinator(tapCallback: self.tapCallback) } func makeUIView(context: UIViewRepresentableContext) -> UIView { let view = UIView() let doubleTapGestureRecognizer = UITapGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleTap(sender:))) /// Set number of touches. doubleTapGestureRecognizer.numberOfTouchesRequired = 2 view.addGestureRecognizer(doubleTapGestureRecognizer) return view } func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext) { } class Coordinator { var tapCallback: (UITapGestureRecognizer) -> Void init(tapCallback: @escaping (UITapGestureRecognizer) -> Void) { self.tapCallback = tapCallback } @objc func handleTap(sender: UITapGestureRecognizer) { self.tapCallback(sender) } } }