Tony Mobile
Tony Mobile

Reputation: 677

How to detect the end of simultaneous gestures? (iOS)

In my app I use UIPinchGestureRecognizer, UIRotationGestureRecognizer and UIPanGestureRecognizer simultaneously for scaling, rotating and moving an image.

The method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: always return YES and the image manipulation works great, but... how can I detect the end of ALL simultaneous gestures, so I can reset the image?

Upvotes: 3

Views: 3097

Answers (1)

ayoy
ayoy

Reputation: 3835

How about a simple solution like counting gestures that are currently being handled, and acting when all of them end?

.h file:

int handledGesturesCount;

.m file:

- (id)init {
    (...)
    handledGesturesCount = 0;
}

// gesture handlers - the code for -pinch: repeats for -pan: and -rotate:
- (void)pinch:(UIPinchGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        handledGesturesCount += 1;
    } else if (recognizer.state == UIGestureRecognizerStateEnded ||
               recognizer.state == UIGestureRecognizerStateCancelled ||
               recognizer.state == UIGestureRecognizerStateFailed)
    {
        handledGesturesCount -= 1;
        if (handledGesturesCount == 0) {
            [self resetImage];
        }
    }
}

- (void)pan:(UIPanGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        handledGesturesCount += 1;
    } else if (recognizer.state == UIGestureRecognizerStateEnded ||
               recognizer.state == UIGestureRecognizerStateCancelled ||
               recognizer.state == UIGestureRecognizerStateFailed)
    {
        handledGesturesCount -= 1;
        if (handledGesturesCount == 0) {
            [self resetImage];
        }
    }
}

- (void)rotate:(UIRotationGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        handledGesturesCount += 1;
    } else if (recognizer.state == UIGestureRecognizerStateEnded ||
               recognizer.state == UIGestureRecognizerStateCancelled ||
               recognizer.state == UIGestureRecognizerStateFailed)
    {
        handledGesturesCount -= 1;
        if (handledGesturesCount == 0) {
            [self resetImage];
        }
    }
}

Upvotes: 7

Related Questions