Alberto Juarez
Alberto Juarez

Reputation: 444

UILongPressGestureRecognizer with different buttons

I have 2 buttons with "UILongPressGestureRecognizer", to do it, i do:

Fot button 1:

-(IBAction)seleccionar46:(id)sender{
UILongPressGestureRecognizer *longpressGesture =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
longpressGesture.minimumPressDuration = 3;
[longpressGesture setDelegate:self];
[self.button1 addGestureRecognizer:longpressGesture];}

For button 2:

    -(IBAction)seleccionar46:(id)sender{
UILongPressGestureRecognizer *longpressGesture =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
longpressGesture.minimumPressDuration = 3;
[longpressGesture setDelegate:self];
[self.button2 addGestureRecognizer:longpressGesture];}

And in "longpressGesture" i need differentiate between button1 and button2, but i can´t do it...

 - (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer{//Here i need do the differentation}

Thanks for all!

Best regards.

Upvotes: 1

Views: 1927

Answers (1)

Daniel
Daniel

Reputation: 22405

What you can do is use the view property of UIGestureRecognizer, so if you save a reference to both your buttons, you can check for equality.

So if you have

@interface blah
{
  UIButton *buttonOne;
  UIButton *buttonTwo;
}

then you add the recognizers to the buttons you can do in the handler

if(gestureRecognizer.view==buttonOne)
{
   //do stuff for button one
}
else if(gestureRecognizer.view==buttonTwo)
{
  //do stuff for button two
}

hope it helps

Upvotes: 6

Related Questions