Search code examples
objective-ctouchuigesturerecognizer

Detecting Pan Gesture End


I've got a view and I applied a UIPanGestureRecogniser to this view:

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAnim:)];
[sliderView addGestureRecognizer:panGesture];
[panGesture release];

I can detect and process the gesture just fine. However, I wish to initiate another method once the gesture has ended.

I know there are two methods that allow this kind of detection. touchesEnded and touchesCancelled however, I've found that touchesCancelled gets called as soon as the touch becomes a gesture i.e. I move my finger enough to warrant a gesture call and touchesEnded rarely, if ever, gets called.

I want to be able to pan left / right and then initiate another function call upon gesture ending. How do I do this?


Solution

  • Pan gesture end event can be detected by checking its state with UIGestureRecognizerStateEnded.

    Check with the below code .

    -(void) panAnim:(UIPanGestureRecognizer*) gestureRecognizer
    {
       if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
       {
          //All fingers are lifted.
       }
    }
    

    From Apple documentation

    A panning gesture is continuous. It begins (UIGestureRecognizerStateBegan) when the minimum number of fingers allowed (minimumNumberOfTouches) has moved enough to be considered a pan. It changes (UIGestureRecognizerStateChanged) when a finger moves while at least the minimum number of fingers are pressed down. It ends (UIGestureRecognizerStateEnded) when all fingers are lifted.

    Read more about UIPanGestureRecognizer