Search code examples
iosuiscrollviewlayoutsubviews

self.decelerating and self.dragging in UIScrollView's layoutSubviews


It appears self.decelerating and self.dragging are not reliable during an overridden layoutSubviews call. For example, they are occasionally both true, which is obviously not possible.

Is there a reliable way to get whether the UIScrollView is decelerating in layoutSubviews?


Solution

  • Having an instance variable set like so fixed it:

    - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
        _decelerating = YES;
    }
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        _decelerating = NO;
    }
    
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
        _decelerating = NO;
    }
    

    The issue is if you start dragging while it's decelerating, self.decelerating is not unset. The above fixes that by taking into account scrollViewWillBeginDragging.