質問

What's the best way to get a button to be capable of both a Touch Up Inside action and a Touch Down Repeat action? Kind of like the Home button on an iPhone: one click goes to the home screen, but two rapid clicks opens the multitasking bar. Right now my button has both methods hooked up, but the Touch Up Inside method (unsurprisingly) gets called before the Touch Down Repeat can happen.

I can come up with a few ways I might pull this off (having the Touch Up Inside method wait a second to see if another click comes before executing, having a second button move invisibly into place after the first click, etc) but they all seem kind of hack-y and open to performance losses and bugs. I found the tapCount property, but it's for UITouch instead of UIButton, so if([_buttonAddItems tapCount] > 1) {} else if([_buttonAddItems tapCount] == 1), which seems like it would be the most efficient way of doing it, doesn't work.

Is there a best-practice for this sort of thing? Or if not, does anyone have a preferred way of getting this done?

役に立ちましたか?

解決

Try disconnecting both touch events from your button, and hook two tap gesture recognizers to it instead. Here is a link to an answer that explains how to set up two gesture recognizers so that one of them recognizes a single tap, and the other one recognizes a double tap:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doSingleTap)];
singleTap.numberOfTapsRequired = 1; 
[myButton addGestureRecognizer:singleTap];

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(doDoubleTap)];
doubleTap.numberOfTapsRequired = 2; 
[myButton  addGestureRecognizer:doubleTap];
// This is the "secret sauce":
[singleTap requireGestureRecognizerToFail:doubleTap];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top