Question

There are three UIButton Events available:

  • UITouchUPInside
  • UITouchDown
  • UITouchDownRepeat

My problem is:

I have a button (Play-Pause). First time its shows play button, if user taps on it, it starts my process. And button image changes to Pause and tag changes to 1.

If user tap on pause button, it pauses the process and image changes to Play and tag to 0.

Now my problem is, what if user taps multiple times, its not executing properly. Like double click on desktop mouse.

I have tried all three above method, but all those times, first two methods are getting called. So how to avoid that.

If its double tap, than also process should be started once only.

Please suggest.

Edit: Currently we are using TouchUpInside event only, there we are checking

if (btn.tag == 0) {

// change to pause button
// start process

btn.tag = 1;
}
else if (btn.tag == 1) {

// change to play button
// stop process

btn.tag = 0;
}

But this is causing issue when user quickly taps on button. Because my process is running on thread, so it takes time to load and stop. Meanwhile use already has double tapped over button.

Was it helpful?

Solution

Perhaps instead of UIButton you should use UIView with 2 UITapGestureRecognizers? One of them would have numberOfTapsRequired = 2, the other one would have numberOfTapsRequired = 1. Use method requiresGestureRecognizerToFail to make sure that you won't have them both calling their target methods. Then you can add any logic you want to the tap handling.

EDIT: Actually, although the above answer is kind of workaround, it will fail if user taps 3 or more times. And since you can't really make a gesture recognizer for every number of taps from 1 to infinity, it will fail in some cases. Try this:

btn.enabled = NO;
if (btn.tag == 0) {

// change to pause button
// start process

btn.tag = 1;
}
else if (btn.tag == 1) {

// change to play button
// stop process

btn.tag = 0;
}
btn.enabled = YES;

OTHER TIPS

You don't need to handle double taps, you are not handling the button properly.

Try this for your button action and set proper images for default/selected states of the button.

- (void)handleButtonAction:(UIButton *)sender {

    [sender setSelected:![sender isSelected]];

    if ([sender isSelected]) {

        //Play

    } else {

        //Pause

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top