我写了一个相当长的动作方法连接到几个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:)];
哪个也不起作用。所以我不知道该怎么做。欢迎任何建议。
答案 0 :(得分:1)
使用:
- (void)tapOnce:(UIGestureRecognizer *)gesture{
[self myMethodwithSender:sender];
}
如果myMethodWithSender
不使用发件人,您可以发送nil。如果它需要它作为按钮,gesture.view
应该是那个按钮(如果您将识别器附加到按钮)