Ser Pounce
Ser Pounce

Reputation: 14527

Is this a safe way to process gestures using one of the UIGestureRecognizers?

Wondering if the following is ok to process UIGestureRecognizer touches:

        if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged)
        {
            //process touches
        }
        else if (pinchGestureRecognizer.state == UIGestureRecognizerStateEnded)
        {
            //do whatever after gesture recognizer finishes
        }

Basically, I'm wondering if UIGestureRecognizerStateEnded occurs should the UIGestureRecognizer still process touches, or at that point have all the touches finished? I'm getting weird values for translationInView so just wanted to ask here.

Upvotes: 1

Views: 115

Answers (1)

Rob
Rob

Reputation: 437552

You asked:

Basically, I'm wondering if UIGestureRecognizerStateEnded occurs should the UIGestureRecognizer still process touches, or at that point have all the touches finished?

When you get UIGestureRecognizerStateEnded, yes, the gesture is done. But obviously, unless you remove the gesture recognizer from the view at that point, if the user starts a new gesture, the gesture recognition process starts over again starting at UIGestureRecognizerStateBegan.

Furthermore, you said:

I'm getting weird values for translationInView so just wanted to ask here.

Your code sample suggests that you're dealing with a pinch gesture, which doesn't do translationInView, so I'm not sure what "weird values" you're getting. You can, though have two simultaneous gestures by setting your gesture's delegate and implementing shouldRecognizeSimultaneouslyWithGestureRecognizer:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self
                                                                                action:@selector(handlePinch:)];
    pinch.delegate = self;
    [self.view addGestureRecognizer:pinch];

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(handlePan:)];
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]])
        return YES;
    if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
        return YES;
    return NO;
}

- (void)handlePinch:(UIPinchGestureRecognizer *)gesture
{
    CGFloat scale = [gesture scale];
    NSLog(@"%s: %@: scale=%.2f", __FUNCTION__, [self stringFromGestureState:gesture.state], scale);
}

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    CGPoint translation = [gesture translationInView:gesture.view];
    NSLog(@"%s: %@: translation=%@", __FUNCTION__, [self stringFromGestureState:gesture.state], NSStringFromCGPoint(translation));
}

The above code works, where the handlePan returns the pan, and the handlePinch returns the pinch, and the translationInView of handlePan looks unexceptional. Perhaps you can show us how you're using a pinch gesture and getting translationInView and tell us what's odd in the values you're getting.

Upvotes: 1

Related Questions