0

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.

Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • Generally user does a single tap only for buttons, if double tap is supported in some cases those are for different behaviour than single tap. What you want is to do same action on both actions. I think you should not make it complex. – Amit Jul 19 '13 at 08:48
  • Check i have added my problem briefly as an Edit; – Mrunal Jul 19 '13 at 08:53
  • I don't think Apple likes it if you do not follow the interface guidelines. Requiring a double tap on a button will confuse users. – Rob van der Veer Jul 19 '13 at 12:14

2 Answers2

4

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;
johnyu
  • 2,152
  • 1
  • 15
  • 33
  • Hi, i have tried this, adding gesture recognizer to uibutton in my custom cell class. But any of those methods are not getting called. – Mrunal Jul 20 '13 at 07:28
  • If you are using UIGestureRecognizers, you don't really need a UIButton - simple UIView would be enough. – johnyu Jul 22 '13 at 09:01
  • I edited my answer, this will probably be a better way to solve your problems. – johnyu Jul 22 '13 at 09:25
0

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

    }

}
Desdenova
  • 5,326
  • 8
  • 37
  • 45