Reputation: 15653
Im slightly confused by a method in the UIGestureRecognizerDelegate protocol. When I implement the delegate method below, I don't seem to ever get my UITapGestureRecognizers sent to this method with their state as UIGestureRecognizerStateRecognized
. They are always in the state UIGestureRecognizerStatePossible
. Is this right?
Below is the test code I used to setup my Tap Gesture and my test implementation of the delegate method:
UITapGestureRecognizer *singelTap = [[UITapGestureRecognizer alloc] initWithTarget:nil action:nil];
singelTap.numberOfTapsRequired = 1;
singelTap.delegate = self;
[self.view addGestureRecognizer:singelTap];
.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
int i = 0;
if(gestureRecognizer.state == UIGestureRecognizerStateBegan){
i=1;
}
if(gestureRecognizer.state == ..... //testing for all possible states...
return YES;
}
Upvotes: 0
Views: 2163
Reputation: 15653
Found this in the documents
This method is called before touchesBegan:withEvent: is called on the gesture recognizer for a new touch.
So I guess the touch can only ever be in the 'possible' state.
Upvotes: 0
Reputation: 502
This is correct. When you implement the
(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
method, you get an opportunity to decide if the UIGestureRecognizer should receive the touch. That means that it has not received the touch yet. Because the UIGestureRecognizer in question has not received anything to do with that particular touch yet, it hasn't been able to determine if it Recognized it or not; thus it remains in the state UIGestureRecognizerStatePossible
during this method.
If you return YES
from the method, the touch will be sent to the UIGestureRecognizer.
Only then will the UIGestureRecognizer handle the touch.
Upvotes: 4
Reputation: 47034
The UIGestureRecognizerDelegate
protocol is only to fine-tune the gesture recognizer's behavior. The actual recognition of events is done by setting a target and a selector:
UITapGestureRecognizer *gr = [[[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleTap:)] autorelease];
// add gr to a view
-(void)handleTap:(UITapGestureRecognizer*)tg
{
if ( tg.state != UIGestureRecognizerStateRecognized ) return;
NSLog(@"tap recognized");
}
In most cases, you can safely ignore the UIGestureRecognizerDelegate
protocol.
Upvotes: 1