如何知道 UICollectionView 是不是会在 scrollToItemAtIndexPath: 被调用后实际滚动?

Posted

技术标签:

【中文标题】如何知道 UICollectionView 是不是会在 scrollToItemAtIndexPath: 被调用后实际滚动?【英文标题】:How to know if UICollectionView will actually scroll after scrollToItemAtIndexPath: is called?如何知道 UICollectionView 是否会在 scrollToItemAtIndexPath: 被调用后实际滚动? 【发布时间】:2016-07-07 09:54:24 【问题描述】:

我使用scrollToItemAtIndexPath:atScrollPosition:animated 滚动UICollectionView。我必须在自动滚动发生时禁用某些功能,然后在滚动结束时重新启用它。当我尝试滚动到某个已经可见且不需要滚动的单元格时,就会出现问题(例如,它是最后一个单元格,并且集合视图已经滚动到最后)。在这种情况下,scrollViewDidEndDraggingscrollViewDidEndDecelerating 都会被调用。

有没有办法知道scrollToItemAtIndexPath 是否真的会滚动UICollectionView

【问题讨论】:

【参考方案1】:

我认为您可以在每次滚动视图滚动时保存最新的滚动视图偏移量并在其中跟踪更改并仅在重大更改后应用操作。

     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
              
    

【讨论】:

我可以使用scrollViewDidEndDragging,但是当滚动甚至没有被触发时该怎么办?我需要知道这一点。 检查这可能有用***.com/questions/18649920/…【参考方案2】:

您可以在调用scrollToItemAtIndexPath 之前根据单元格的框架确定scrollToItemAtIndexPath 是否会滚动。例如,如果您只想让单元格可见 (UICollectionViewScrollPositionNone):

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

很遗憾,scrollToItemAtIndexPath 并没有简单地告诉你它是否会滚动。

【讨论】:

以上是关于如何知道 UICollectionView 是不是会在 scrollToItemAtIndexPath: 被调用后实际滚动?的主要内容,如果未能解决你的问题,请参考以下文章

如何检测滚动到水平 UICollectionView 的末尾

UICollectionView 页眉和页脚视图

如何知道 UICollectionView 何时完成布局?

使用数组项属性值在 UITableView/UICollectionView 中显示

如何检测最后一个单元格是不是在 UICollectionView 中可见?

如何让 UICollectionView 连续水平滚动而不是创建第二行?