user5480023
user5480023

Reputation:

Tap gesture fires on single tap before conforming to double tap requirement

I've looked at previous answers to this question, but the solutions don't seem to work in my case (e.g. adding a requirement for the gesture recognizer to fail).

I have hooked up my view to a tap gesture recognizer, and I'm trying to use the following handler in my code:

@IBAction func doubleTapView(gesture: UITapGestureRecognizer) {
    gesture.numberOfTapsRequired = 2

    if gesture.state == .Ended {
        print("Works")
    }
}

This is what happens:

The first tap will print "Works". From then on, it will require the double-tap to print "Works"

How do I eliminate it printing on the first tap before it defaults to the correct and intended behaviour?

Upvotes: 0

Views: 142

Answers (1)

Raja Vikram
Raja Vikram

Reputation: 1980

You need to set the number of touches before the @IBAction function call.

There are two ways here:

Either in storyboard

Set it here

Or create an IBOutlet and set it in the viewDidLoad() function.

@IBOutlet weak var tapGesture: UITapGestureRecognizer!

override func viewDidLoad() {
    super.viewDidLoad()
    tapGesture.numberOfTapsRequired = 2
}

Upvotes: 1

Related Questions