Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add gesture recognizers to multiple buttons?

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'?

like image 578
Nik's Avatar asked Oct 13 '11 09:10

Nik's


People also ask

How do you add a tap gesture to a label?

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.

How to use tap gesture recognizer Xamarin forms?

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.

What is Pan gesture?

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.

What is UITapGestureRecognizer Swift?

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.


2 Answers

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.

like image 22
EmptyStack Avatar answered Sep 28 '22 21:09

EmptyStack


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.

like image 124
Aberrant Avatar answered Sep 28 '22 21:09

Aberrant