Hi, I am trying to add gesture recognizers to 'UIButton'. When I do it like this:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.LeftBottomSpaceBtn addGestureRecognizer:singleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
[singleTap release];
It works properly, but when I tried to add this gesture to multiple buttons it did not work:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.LeftBottomSpaceBtn addGestureRecognizer:singleTap];
[self.LeftUpSpaceBtn addGestureRecognizer:singleTap];
[self.RightBUpSpaceBtn addGestureRecognizer:singleTap];
[self.LeftReturnBtn addGestureRecognizer:singleTap];
[self.RightReturnBtn addGestureRecognizer:singleTap];
[self.DeleteBtn addGestureRecognizer:singleTap];
[self.CapsBtn addGestureRecognizer:singleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
[singleTap release];
So how can I add the same gesture to multiple buttons in the same way I have added the 'longPress' and 'doubleTap'?
Create a new project and name it How To Add Gesture to UILabel. // Do any additional setup after loading the view. We just created a setupLabelTap() function where we've created a UITapGestureRecognizer which will fire a labelTapped() function. It is simple.
The tap gesture is used for tap detection and is implemented with the TapGestureRecognizer class. var tapGestureRecognizer = new TapGestureRecognizer(); tapGestureRecognizer. Tapped += (s, e) => { // handle the tap }; image. GestureRecognizers.
A pan gesture occurs any time a person moves one or more fingers around the screen. A screen-edge pan gesture is a specialized pan gesture that originates from the edge of the screen. Use the UIPanGestureRecognizer class for pan gestures and the UIScreenEdgePanGestureRecognizer class for screen-edge pan gestures.
UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer . For gesture recognition, the specified number of fingers must tap the view a specified number of times. Although taps are discrete gestures, they're discrete for each state of the gesture recognizer.
You can add one gesture recognizer to one view alone. If you add it to more than one views, the last added view will be added with the recognizer.
Create different instances of gesture recognizers and add them to individual views.
I'd suggest the following:
NSMutableSet *buttons = [[NSMutableSet alloc] init];
[buttons addObject: self.LeftBottomSpaceBtn];
[buttons addObject: self.LeftUpSpaceBtn];
[buttons addObject: self.RightBUpSpaceBtn];
[buttons addObject: self.LeftReturnBtn];
[buttons addObject: self.RightReturnBtn];
[buttons addObject: self.DeleteBtn];
[buttons addObject: self.CapsBtn];
for(UIButton *button in buttons)
{
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[button addGestureRecognizer:singleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
[singleTap release];
}
If you save the set as a variable, you can do other stuff for all buttons as well, such as releasing them all and changing all their backgroundColors, without calling them all individually.
You will probably need to make seperate doubletaprecognizers for each button as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With