4

I have create several custom UIGestureRecognizers without issue. I decided I would like to have a custom version of single tap gesture and went about subclassing UIGestureRecognizer. All seems fine except for one issue. In my action handler [gestureRecognizer locationInView:self] always returns zero for both x and y. When I go back to UITapGestureRecognizer the action handler works fine. This must be something to do with the subclassed gesture recognizer, here is my code:

#import "gr_TapSingle.h"

#define tap_Timeout 0.25

@implementation gr_TapSingle


- (id)init
{
    self = [super init];
    if ( self )
    {
    }
    return self;
}

- (void)reset
{
}

-(void)gesture_Fail
{
    self.state = UIGestureRecognizerStateFailed;
}

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    [super touchesBegan:touches withEvent:event];

    if ( [self numberOfTouches] > 1 )
    {
        self.state = UIGestureRecognizerStateFailed;
        return;
    }

    originLocation = [[[event allTouches] anyObject] locationInView:self.view];

    [self performSelector:@selector(gesture_Fail) withObject:nil afterDelay:tap_Timeout];
}

-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    [super touchesMoved:touches withEvent:event];

    if ( self.state == UIGestureRecognizerStatePossible )
    {
        CGPoint l_Location = [[[event allTouches] anyObject] locationInView:self.view];
        CGPoint l_Location_Delta = CGPointMake( l_Location.x - originLocation.x, l_Location.y - originLocation.y );
        CGFloat l_Distance_Delta = sqrt( l_Location_Delta.x * l_Location_Delta.x + l_Location_Delta.y * l_Location_Delta.y );
        if ( l_Distance_Delta > 15 )
            self.state = UIGestureRecognizerStateFailed;
        return;
    }
}

-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
    [super touchesEnded:touches withEvent:event];

    if ( self.state == UIGestureRecognizerStatePossible )
        [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(gesture_Fail) object:nil];

    if ( self.state != UIGestureRecognizerStateFailed )
        self.state = UIGestureRecognizerStateEnded;
}

-(void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
{
    [super touchesCancelled:touches withEvent:event];
    if ( self.state == UIGestureRecognizerStatePossible )
        [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(gesture_Fail) object:nil];
    self.state = UIGestureRecognizerStateFailed;
}

@end

1 Answer 1

2

Apple's docs say:

The returned value is a generic single-point location for the gesture computed by the UIKit framework. It is usually the centroid of the touches involved in the gesture. For objects of the UISwipeGestureRecognizer and UITapGestureRecognizer classes, the location returned by this method has a significance special to the gesture. This significance is documented in the reference for those classes.

So I assume every subclass has it's own special implementation of this method, fitting its own specialties. So you have to implement this on your own if you want to subclass UIGestureRecognizer.

EDIT:

Something like:

- (CGPoint)locationInView:(UIView *)view
{
   if(view == self.view)
   {
      return originLocation;
   }
   else
   {
     //you decide
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.