UILongPressGestureRecognizer具有不同的按钮

Alb*_*rez 1 iphone xcode button

我有两个带"UILongPressGestureRecognizer"的按钮,为此,我这样做:

Fot按钮1:

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

对于按钮2:

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

在"longpressGesture"中,我需要区分button1和button2,但我无法做到......

 - (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer{//Here i need do the differentation}
Run Code Online (Sandbox Code Playgroud)

谢谢大家!

最好的祝福.

Dan*_*iel 6

你可以做的是使用view属性UIGestureRecognizer,所以如果保存对两个按钮的引用,你可以检查是否相等.

所以,如果你有

@interface blah
{
  UIButton *buttonOne;
  UIButton *buttonTwo;
}
Run Code Online (Sandbox Code Playgroud)

然后将识别器添加到您可以在处理程序中执行的按钮

if(gestureRecognizer.view==buttonOne)
{
   //do stuff for button one
}
else if(gestureRecognizer.view==buttonTwo)
{
  //do stuff for button two
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你