I wrote a fairly long action method connected to several UIButton
s, and now I've realised I would like to have something different happen if I tap one of the buttons twice. So I am setting up two gesture recognizers:
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];
then, I implement the methods:
- (void)tapOnce:(UIGestureRecognizer *)gesture{
//Do what you were already doing
}
- (void)tapTwice:(UIGestureRecognizer *)gesture{
// Some new functionality
}
Now, I don't have a problem with the new functionality. Where I get stuck is trying to get the tapOnce:
method to do what my button was already doing. I need to know which button was tapped, so I can't use this:
[myButton sendActionsForControlEvents: UIControlEventTouchUpInside];
I also tried the old:
for (button in myArrayOfButtons){
[button sendActionsForControlEvents: UIControlEventTouchUpInside];
}
No luck either.
Then I went the way of actually creating a separate method for the implementation of my button, so what was:
- (IBAction)buttonPressed:(id)sender {
//my functionality...
}
became:
- (IBAction)buttonPressed:(id)sender {
[self myMethodwithSender:sender];
}
-(void)myMethodwithSender: (id)sender{
// my functionality
}
So that worked in itself, but when I try to put the tap once and twice into play I get stuck. I tried putting my new method in the method for tap once:
- (void)tapOnce:(UIGestureRecognizer *)gesture{
[self myMethodwithSender:sender];
}
but of course there is no sender in this method so it doesn't work (right?)
Then I tried changing this:
UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce:)];
to this:
UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myMethodwithSender:)];
Which doesn't work either. So I am not sure how to do this. Any suggestions welcome.
With:
- (void)tapOnce:(UIGestureRecognizer *)gesture{
[self myMethodwithSender:sender];
}
If myMethodWithSender
doesn't use sender, you can send nil. If it needs it to be the button, gesture.view
should be that button (if you attached the recognizer to the button)