从 UIGestureRecognizer 的操作方法中获取被双击的 UIButton

Getting the UIButton that was double-tapped from UIGestureRecognizer's action method

我写了一个连接到多个 UIButton 的相当长的操作方法,现在我意识到如果我点击其中一个按钮两次,我希望发生一些不同的事情。所以我正在设置两个手势识别器:

UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapOnce:)];
UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapTwice:)];


tapOnce.numberOfTapsRequired = 1;
tapTwice.numberOfTapsRequired = 2;

[tapOnce requireGestureRecognizerToFail:tapTwice];

[self.mybutton addGestureRecognizer:tapOnce];
[self.mybutton addGestureRecognizer:tapTwice];

然后,我实现方法:

- (void)tapOnce:(UIGestureRecognizer *)gesture{
   //Do what you were already doing
}


- (void)tapTwice:(UIGestureRecognizer *)gesture{ 
  // Some new functionality
}

现在,我对新功能没有任何问题。我被卡住的地方是试图让 tapOnce: 方法来做我的按钮已经在做的事情。我需要知道点击了哪个按钮,所以我不能使用这个:

[myButton sendActionsForControlEvents: UIControlEventTouchUpInside];

我也试过旧的:

for (button in myArrayOfButtons){
    [button sendActionsForControlEvents: UIControlEventTouchUpInside];
}

也不走运。

然后我实际创建了一个单独的方法来实现我的按钮,所以是:

- (IBAction)buttonPressed:(id)sender {

   //my functionality...
}

变成了:

- (IBAction)buttonPressed:(id)sender {

    [self myMethodwithSender:sender];
}

-(void)myMethodwithSender: (id)sender{

    // my functionality
 }

所以这本身就起作用了,但是当我尝试一次又一次地打开水龙头时,我卡住了。我试着把我的新方法放在点击一次的方法中:

- (void)tapOnce:(UIGestureRecognizer *)gesture{

       [self myMethodwithSender:sender];
}

但是这个方法当然没有发件人所以它不起作用(对吧?)

然后我试着改变这个:

UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapOnce:)];

对此:

UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(myMethodwithSender:)];

这也不管用。所以我不知道该怎么做。欢迎提出任何建议。

与:

- (void)tapOnce:(UIGestureRecognizer *)gesture{
   [self myMethodwithSender:sender];
}

如果myMethodWithSender不使用sender,你可以发送nil。如果它需要它作为按钮,gesture.view 应该是那个按钮(如果你将识别器附加到按钮)