Default UIGestureStateEnded detection for UIScrollView 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;
    }
}

      

Started to work well. But mine NSLog

shows mine end

all the time when scrolling (as the state should be changed). What is the correct way to determine the end state of a gesture recognizer?

+3


source to share


2 answers


Have you considered using and implementing the "normal" methods of the UIScrollViewDelegate protocol? they should be enough for your purposes, if you don't need to mention anything in your question yet:



- (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");
}

      

+1


source


The code you showed us behaves exactly as it was designed. I think you are expecting something that will not happen.

More precisely. The GestureRecognizer only recognizes the physical gesture that the user makes on the screen. Thus, small gestures are repeated, with the events of the beginning and the end being repeated many times. If you make a gesture by touching and holding and moving slowly back and forth, you should only see one end - when the user releases.



But @meronix is ​​right in saying that this is similar to what you would expect from a gesture recognizer - this is when the scrollview stops scrolling, which can be long after the custom gesture has finished.

0


source







All Articles