UIButton inside a view that has a UITapGestureRecognizer
You can set your controller or view (whichever creates the gesture recognizer) as the delegate of the UITapGestureRecognizer
. Then in the delegate you can implement -gestureRecognizer:shouldReceiveTouch:
. In your implementation you can test if the touch belongs to your new subview, and if it does, instruct the gesture recognizer to ignore it. Something like the following:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// test if our control subview is on-screen
if (self.controlSubview.superview != nil) {
if ([touch.view isDescendantOfView:self.controlSubview]) {
// we touched our control surface
return NO; // ignore the touch
}
}
return YES; // handle the touch
}
UITapGestureRecognizer blocks touch event for UIButton in subview
I found a solution to this by using below code in GestureRecogniser delegate method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isDescendantOfView:IFView.stickerPreviewView]) {
return NO;
}
return YES;
}
It specifies exactly what subview in this case IFView.stickerPreviewView is to return NO. Also in InputFunctionView, use this instead to add the subview:
[self.superview addSubview:_stickerPreviewView];
UITapGestureRecognizer on a UIButton
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
}
UIButton subview of a gesture recognizer
Could you manually set [button setHighlighted:YES]
when the tap gesture is first recognized and its location matches that of the button's, and then to NO
when the gesture ends?
Single tap, created by UITapGestureRecognizer, doesn't work on UIButton
If you want to add TapGesture into Button then do it like this way:
let singleTap = UITapGestureRecognizer(target: self, action: "singleTap:")
singleTap.numberOfTapsRequired = 1
singleTap.numberOfTouchesRequired = 1
yourButton.addGestureRecognizer(singleTap)
UIButton Click Event not working if I add UITapGestureRecognizer in swift
Don't add the gesture to main viewController's view add a full screen subview below UIButton and add the gesture to it
UITapGestureRecognizer not working with UIButton
from apple docs:
Although taps are discrete gestures, they are discrete for each state of the gesture recognizer; thus the associated action message is sent when the gesture begins and is sent for each intermediate state until (and including) the ending state of the gesture. Code that handles tap gestures should therefore test for the state of the gesture, for example:
- (void)handleTap:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded)
{
// handling code
}
}
if you NSLog(@"%d",sender.state) inside the method (before the if statement) you'll see that the method is getting fired only with the UIGestureRecognizerStateEnded state, thus you should change your
if (PressRecognizer.state == UIGestureRecognizerStateBegan)
to
if (PressRecognizer.state == UIGestureRecognizerStateEnded)
UIButton not working when single tap gesture is added to superview
Your problem is similar to this question which hopefully has been answered ;)
UIButton inside a view that has a UITapGestureRecognizer
Related Topics
Ios 6: How to Restrict Some Views to Portrait and Allow Others to Rotate
Dismiss Keyboard by Touching Background of Uitableview
How to Add Multi-Line Text to a Uibutton
Run Code Only After Asynchronous Function Finishes Executing
Undocumented Nsurlerrordomain Error Codes (-1001, -1003 and -1004) Using Storekit
Ios: How to Change App Language Programmatically Without Restarting the App
Programmatically Open Maps App in iOS 6
Unexpected Cfbundleexecutable Key
Ios: Issue with Ampersand in the Url String
Warning :-Presenting View Controllers on Detached View Controllers Is Discouraged
Move a View Up Only When the Keyboard Covers an Input Field
Ios Universal Links Are Not Opening In-App
Swrevealviewcontroller Without Using Navigationcontroller
iOS 7 Tableview Like in Settings App on iPad