Recognizing Longpress gestures on two Uibuttons in ios

I have two UIbuttons and I want to implement Longpressgesture on both.

So, I wrote the code below.

-(void)viewdidLoad { UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonLongPressed:)]; longPress.minimumPressDuration = 0.5; [Button1 addGestureRecognizer:longPress]; [Button2 addGestureRecognizer:longPress]; } - (void)buttonLongPressed:(UILongPressGestureRecognizer *)sender { if(sender.state == UIGestureRecognizerStateBegan) { } } 

Now I doubt how to check which button is pressed?

Thanks Ranjit

+4
source share
1 answer

First, note that the gesture recognizer should only be attached to view one . You must create a new instance for each button.

To answer your question, you can add tag values ​​to your buttons:

 Button1.tag = 1000; Button2.tag = 1001; 

Then check them in the recognizer:

 UIView *view = sender.view; int tag = view.tag; if (tag == 1000) { ... } 

You can enter any tag values, but I often start with a value as high as 1000 to avoid collisions with any other tags that I assign in Interface Builder.

Another option is to use a different processing function for each recognizer.

+6
source

Source: https://habr.com/ru/post/1382855/


All Articles