滚动到 UICollectionView 中的下一个单元格 [英] Scrolling to next cell in UICollectionView
问题描述
我在容器视图中有一个 UICollectionView.它的大小使我一次只能看到一个单元格.我已禁用垂直滚动,因此只发生水平滚动.
I have a UICollectionView in a container View. It is sized in such a way that I see only one cell at a time. I have disabled vertical scrolling so only horizontal scrolling takes place.
在我水平滚动单元格的地方,一切都很好.这种滚动的缺点是我可以滚动并到达我看到两个单元格的一半部分的位置.
Everything works just peachy where I scroll horizontally across cells. The disadvantage of this scrolling is I can scroll and get to a position where I see half portion of two cells with a gap in between.
我想要实现的是一次只显示一个单元格,而从不显示两个半单元格.所以滚动应该一次带我到下一个单元格.
What I want to achieve is to show only one cell at a time and never show two half cells. So the scrolling should take me to the next cell one at a time.
希望这是有道理的.
如果有人尝试过或可以帮助我实现相同的目标,请告诉我.谢谢.
Please let me know if anyone has tried or can help me to achieve the same. Thanks.
推荐答案
您可以通过设置 pagingEnabled = YES
一次滚动一个单元格.或者对于常规滚动,您可以通过覆盖 UICollectionViewLayout
的 targetContentOffsetForProposedContentOffset:withScrollingVelocity:
来调整减速结束的位置.使用后一种方法时,您通常会执行以下操作:
You can scroll one cell at a time by setting pagingEnabled = YES
. Or for regular scrolling, you can adjust where the deceleration ends by overriding targetContentOffsetForProposedContentOffset:withScrollingVelocity:
of UICollectionViewLayout
. When using the later method, you would typically do the following:
- 确定最接近
proposedContentOffset
的索引路径.如果您的单元格之间没有间隙,您可以使用[UICollectionView indexPathForItemAtPoint:]
.否则,您可能需要以某种方式检查您的布局,以确定要滚动到哪个索引路径. - 通过获取索引路径的布局属性来确定要滚动到的索引路径的框架
- 确定将给定框架定位到所需位置的内容偏移量.
- Determine the nearest index path to
proposedContentOffset
. If there are no gaps between your cells, you can use[UICollectionView indexPathForItemAtPoint:]
. Otherwise, you may have to inspect your layout in some way to determine which index path you want to scroll to. - Determine the frame of the index path you want to scroll to by getting the layout attributes for that index path
- Determine the content offset that will position the given frame where you want it.
这篇关于滚动到 UICollectionView 中的下一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!