1

I use scrollToItemAtIndexPath:atScrollPosition:animated to scroll UICollectionView. I have to disable some functionality while this automatic scrolling is happening and then re-enable it, when scrolling ends. The problem occurs, when I try to scroll to some cell which is already visible and there is no scroll necessary (for example, it's the last cell and collection view is already scrolled until the very end). In this case scrollViewDidEndDragging nor scrollViewDidEndDecelerating are called.

Is there a way to know if scrollToItemAtIndexPath will actually scroll UICollectionView?

Jaroslav
  • 1,309
  • 13
  • 26

2 Answers2

0

I Think you can save the latest scroll view offset & track changes in it every time the scrollView scrolls & apply actions after significant changes only.

     func scrollViewDidScroll(scrollView: UIScrollView) {

             let scrollOffset = scrollView.contentOffset.y;

             if scrollOffset < (previousValue-100) || scrollOffset > (previousValue+100) 
              {
               // Scrolled up or down with 100 points which you can change
               previousValue = scrollOffset
              }
    }
Mohamed Mostafa
  • 997
  • 6
  • 12
  • I can use `scrollViewDidEndDragging` for that, but what to do when the scroll isn't even fired? I need to know that. – Jaroslav Jul 07 '16 at 11:31
  • check this may be useful http://stackoverflow.com/questions/18649920/uicollectionview-current-visible-cell-index – Mohamed Mostafa Jul 07 '16 at 19:35
0

You can determine whether scrollToItemAtIndexPath will scroll based on the cell's frame before calling scrollToItemAtIndexPath. For example, if you just want the cell to be visible (UICollectionViewScrollPositionNone):

UICollectionViewLayoutAttributes* attrs = [collectionView layoutAttributesForItemAtIndexPath:indexPath];
CGRect frame = [collectionView convertRect:attrs.frame toView:self.view];
BOOL willScroll = !CGRectContainsRect(collectionView.frame, frame);

It's unfortunate that scrollToItemAtIndexPath doesn't simply tell you whether it will scroll.

Taylor
  • 4,661
  • 2
  • 24
  • 46