Question

I found a post on here that briefly described how to tell if a UIScrollView has finished scrolling How to know exactly when a UIScrollView's scrolling has stopped?. It uses the scrollViewDidEndDragging function and as specified in the UIScrollView documentation and post mentioned above, the willDecelerate:(BOOL)decelerate parameter to tell when the scroll has stopped. However, I still can't seem to get this code snippet to work:

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"%d", decelerate);

    if (!decelerate)
    {
        isScrolling = NO;
    }

    isScrolling = YES;
}

I confirm that the function is being executed with the NSLog but even after I let go and scrolling is finished decelerate is still equal to 1. Why is this?

Était-ce utile?

La solution

-scrollViewDidEndDragging:willDecelerate: is called after you stop dragging, but that's not necessarily when the view stops scrolling. If you want to know when the view stops moving, use -scrollViewDidEndDecelerating: or -scrollViewDidEndScrollingAnimation:.

If you only want to know whether the scroll view is currently scrolling, it may be better to ask the scroll view itself instead of trying to keep track of when it starts and when it stops. If the scroll view is scrolling, its either because the user is actively dragging, or because the scroll view is decelerating after a drag. You can easily add a method (in a category) to UIScrollView that checks both of those conditions:

@interface UIScrollView (Scrolling)
- (BOOL)scrolling;
@end

@implementation UIScrollView (Scrolling)
- (BOOL)scrolling
{
    return (self.dragging || self.decelerating);
}
@end
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top