1
votes

I am using the following two UIScrollView delegate methods to call another method in my UIViewController:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // do something
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    // do something
}

I've tried various different ways of calling //do something but either end up with both not being called, or both delegate methods being called, calling //do something twice in certain situations. For example:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if(![scrollView isDecelerating] && ![scrollView isDragging]){
        //do something
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if(!decelerate){
        //do something
    }
}

So with the above if I scroll and let it slow to a stop, it calls scrollViewDidEndDecelerating:, but if I scroll and stop it with a tap it calls both scrollViewDidEndDragging: and scrollViewDidEndDecelerating:

I want it to call one or the other... is there something I can do with scrollViewDidEndDecelerating: and the scrollView object to stop this double method call?

3
Can you be a little more clear on what exactly you want? - Praveen S
Can't you just try scrollViewDidScroll method instead if you dont have any complex things to do? - visakh7
scrollViewDidScroll is too heavy. I've posted the solution below. I basically want a method to fire when scrolling stops either by decelerating naturally, or when the user stops a drag operation. - mootymoots

3 Answers

2
votes

The scroll view delegate does its job. You can not prevent it from happening. But with a simple logic you can achieve what you are trying to do.

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    dragged = YES;
    // do something
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (!dragged) {

         // do something
    }

    dragged = NO;
}
2
votes

// very simple

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    if(!decelerate){
         // Do something 
    }

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
       // Do something 
}
0
votes

This seems to work. Close to EmptyStack's answer

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (!dragged) {
        //do something
    }

    dragged = NO;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    if(!decelerate){
        dragged = YES;
        //do something
    } else {
        dragged = NO;
    }
}