简体   繁体   中英

Detecting UIGestureStateEnded in default UIScrollView pan gesture recognizer

My code:

[self.scrollView.panGestureRecognizer addTarget:self action:@selector(handlePanForScrollView:)];

- (void)handlePanForScrollView:(UIPanGestureRecognizer *)gesture {
switch (gesture.state) {
    case UIGestureRecognizerStateBegan:
        startScrollPoint = [gesture locationInView:self.scrollView];
        break;
    case UIGestureRecognizerStateEnded: {
        NSLog(@"end");
    }
    default:
        ;
        break;
    }
}

Began state works fine. But my NSLog shows my end all time while scrolling (as it should be state changed). What is the right way to detect the end state of gesture recognizer?

did you consider to use and implement the "normal" methods of a UIScrollViewDelegate protocol? they should be enough for your uses, if you don't need else not mentioned in your question:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"scrolling now");
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"stop scrolling");
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"going to scroll");
}

The code you showed us is behaving exactly as designed. I think you are expecting something that will not happen.

To be more precise. the GestureRecognizer only recognixes the physical gesture that the user makes on the screen. Thus there are repeated small gestures happening, with the began and end states happening repeatedly. If you make the gesture by touching and holding and moving slowly back and forth, you should see only one end - when the user lets go.

But @meronix is correct in saying that it sounds like what you are expecting from the gesture recognizer is when the scrollview stops scrolling, which could be long after the user's gesture has finished.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM