Search code examples
iphoneiosuibuttonuitapgesturerecognizer

Count number of taps with UITapGestureRecognizer


I have a button that increases a quantity, and when I tap it, I make a service call. The problem is that if I tap it multiple times I only want to make the service call after I finish tapping it. I tried using a UITapGestureRecognizer but I don't know how to count the number of taps.

Any help would be appreciated.

Here is the sample code

- (void)quantityChanged:(UITapGestureRecognizer *)tapRecognizer {
    static NSUInteger numberOfTaps;
    if (tapRecognizer.state == UIGestureRecognizerStateBegan) {
       numberOfTaps = 0;
    }
    if (tapRecognizer.state == UIGestureRecognizerStateEnded)
        NSLog(@"%d",numberOfTaps);
    if (tapRecognizer.state == UIGestureRecognizerStateChanged)
        numberOfTaps++;

}


Solution

  • Ok. So I found the solution to my problem. I will perform my web service call with a delay of 0.5 seconds, and each time I press on my button I cancel all the selectors that are waiting to be performed and a call again performSelector: withDelay.