Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

iphone - UITapGestureRecognizer on a UIButton

I have a UIButton which has an IBAction as well as a UITapGestureRecognizer to detect double taps.

At the moment it looks like the IBAction is blocking the recognizer. Is there a way to stop this or do UITapGestureRecognizer even work on buttons? If so wouldn't it be better to just add the recognizers and remove the IBActions?

EDIT

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[A1 addGestureRecognizer:doubleTap];
[A2 addGestureRecognizer:doubleTap];
[B1 addGestureRecognizer:doubleTap];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like you are trying to attach one gesture recognizer to multiple buttons. A gesture recognizer can only be attached to one view at a time. So in your case, the last button you are attaching the recognizer to (button B1) probably responds to the double tap but A1 and A2 don't.

Create a separate recognizer for each button.
But all three recognizers can call the same action method (handleDoubleTap:).

However, when you try to do a single tap on a button, there will be a slight delay as it waits to see if it's the beginning of a double tap. There are ways to reduce the delay but may not be worth it if you can live with the delay and the workarounds bring up other issues.

Edit:
In your comment, you say you "want to detect if they are pressed at the same time". To do this, you don't need gesture recognizers. You can just use the standard control events provided.

Next, in IB, for each button, hook up the "Touch Down" event with buttonPressed:. Or, to do it programmatically:

[button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button2 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button3 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];

Next, in IB, for each button, hook up the "Touch Up Inside" and "Touch Up Outside" events with buttonReleased:. Or, to do it programmatically:

[button1 addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
[button2 addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
[button3 addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];

Next, add ivars to keep track of how many or which buttons are pressed:

@property (nonatomic) int numberOfButtonsBeingTouched;
@property (strong, nonatomic) NSMutableSet *buttonsBeingTouched; //alloc + init in viewDidLoad or similar

If you just care how many buttons are pressed, you don't need the NSMutableSet.

Finally, add the buttonPressed and buttonReleased methods:

- (IBAction)buttonPressed:(UIButton *)button {
    self.numberOfButtonsBeingTouched++;
    [self.buttonsBeingTouched addObject:button];
    //your logic here (if (self.numberOfButtonsBeingTouched == 3) ...)
}

- (IBAction)buttonReleased:(UIButton *)button {
    self.numberOfButtonsBeingTouched--;
    [self.buttonsBeingTouched removeObject:button];
    //your logic (if any needed) here
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...