Search code examples
iosiphoneswiftgesturecs193p

Double tap gesture function is being fired after 1 tap only on first tap, every other instance it requires 2 taps


As of right now, when I run my program and tap, I am getting the println() message after one tap. However, after the first println() message, I have to double tap for every following println() message.

I want it so every time I have to double tap (including the first time).

In my View Controller I have the following:

@IBOutlet weak var graphview: GraphView! {
    didSet {
        //
        graphview.addGestureRecognizer(UITapGestureRecognizer(target: graphview, action: "doubleTap:"))
    }
}

And my function in the View is the following:

func doubleTap(gesture: UITapGestureRecognizer)
{
    gesture.numberOfTapsRequired = 2
    println("hit twice")

}

Solution

  • You have to set

    gesture.numberOfTapsRequired = 2
    

    before adding the gesture recognizer to the view, i.e.

    @IBOutlet weak var graphview: GraphView! {
        didSet {
            var doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: graphview, action: "doubleTap:")
            doubleTap.numberOfTapsRequired = 2
            graphview.addGestureRecognizer(doubleTap)
        }
    }
    

    and

    func doubleTap(gesture: UITapGestureRecognizer)
    {
        println("hit twice")
    }