How to stop UIPanGestureRecognizer when object moved to certain frame

情到浓时终转凉″ 提交于 2019-12-03 05:19:53

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;

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;
    }
}

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

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
     //return NO when you reach the frame
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!