Two finger tap gesture in SwiftUI

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

Accepted Reply

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)
        }
    }
}

Replies

I found nowhere in doc the equivalent of UIKit numberOfTouches.


I don't know if you could create a UIKit gesture and use it here.

May have a look here for some hint:

https://stackoverflow.com/questions/57577901/how-to-handle-touch-gestures-in-swiftui-in-swift-uikit-map-component

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)
        }
    }
}