0
votes

I would like to create a pan gesture recognizer and attach it to a view.

The gesture should only take into consideration movement on one of the axises X or Y.

What I have so far is pretty crude

        UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        [self addGestureRecognizer:recognizer];

-(void)handlePan:(UIPanGestureRecognizer *)recognizer
{
    UIGestureRecognizerState state = recognizer.state;
    CGPoint translation = [recognizer translationInView:self];
    NSLog(@"state = %d : panned to %@",state,NSStringFromCGPoint(translation));


    if ((recognizer.state == UIGestureRecognizerStateChanged) ||
        (recognizer.state == UIGestureRecognizerStateEnded))
    {
        CGFloat translationX = translation.x;
        if (ABS(translation.y) > 20 && ABS(translation.y) > ABS(translation.x))
        {
            // this is not a valid x pan because the vector has to much "y" movement in it
        }
        // need to do similar for x axis
    }
}

Is there a better way to achieve this?

1

1 Answers

0
votes

If you don't mind embedding your view in a ScrollView then you can just set directionalLockEnabled to YES and that will take care of it. You can also take out the scroll indicators to make it look the same as it would with a UIPanGestureRecognizer.