16

I have an object of image type which I am moving using UIPanGestureRecognizer, and I need to stop recognizing the UIPanGestureRecognizer when the object reaches a certain frame.

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [templatePhotoPlaceholderView addGestureRecognizer:panRecognizer];

-(void)move:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint translatedPoint = [gestureRecognizer translationInView:templatePhotoPlaceholderView];

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        _firstX = [imageview center].x;
        _firstY = [imageview center].y;
    }



    translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);
    //NSLog(@" Move center point :%@", NSStringFromCGPoint(translatedPoint));

    [imageview setCenter:translatedPoint];  

}

How may I do this ?

Oliver
  • 23,072
  • 33
  • 138
  • 230
user905582
  • 524
  • 1
  • 6
  • 16

3 Answers3

23

UIGestureRecognizers have an enabled property. Documentation:

Disables a gesture recognizers so it does not receive touches. The default value is YES. If you change this property to NO while a gesture recognizer is currently recognizing a gesture, the gesture recognizer transitions to a cancelled state.

EDIT:

Just set the enabled property to NO.

gestureRecognizer.enabled = NO;
jbat100
  • 16,757
  • 4
  • 45
  • 70
17

When you need to stop your UIPanGestureRecognizer from recognizing gesture, you just put this code line (as jbat100 said) in -(void)move:(UIPanGestureRecognizer *)gestureRecognizer:

gestureRecognizer.enabled = NO;

after this line your gestureRecognizer state set as "UIGestureRecognizerStateCancelled"

then just add couple lines to your -(void)move:(UIPanGestureRecognizer *)gestureRecognizer function:

if ([gestureRecognizer state] == UIGestureRecognizerStateCancelled) {
     gestureRecognizer.enabled = YES;
}

and you'll be able to work with your gesture recognizer

EDIT:

Here's code snippet:

- (void)move:(UIPanGestureRecognizer *)gestureRecognizer {
    BOOL cancelPanGesture = YES;
    if (cancelPanGesture) {
        /* 
         After this line gesture recognizer will be disabled, state will be UIGestureRecognizerStateCancelled
         and this method (move:) will fire one more time.
         */
        gestureRecognizer.enabled = NO;
    }
    if (gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
        gestureRecognizer.enabled = YES;
    }
}
derpoliuk
  • 1,756
  • 2
  • 27
  • 43
  • 1
    Answer did not make sense where exactly `gestureRecognizer.enabled = YES;` should be written. If you can elaborate would make much more sense. – rohan-patel Jun 03 '14 at 16:07
  • 2
    I love this. It keeps the state of the recognizer encapsulated in that function instead of putting stupid `shouldCancelPan` instance variables in your code. – John Nesbitt Feb 04 '16 at 14:32
  • using this technique when i reach a certain point and set recogniser.enabled = false it moves to a cancelled state wherein i again enable it but the gesture doesent start untill i raise my finger and start again. – Pranav Gupta Mar 15 '18 at 07:50
5

Set the pangesture.delegate=self, and implement delegate method

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
     //return NO when you reach the frame
}
nova
  • 781
  • 11
  • 17