简体   繁体   English

滚动到 UICollectionView 中的下一个单元格

[英]Scrolling to next cell in UICollectionView

I have a UICollectionView in a container View.我在容器视图中有一个 UICollectionView。 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.所以滚动应该一次带我到下一个单元格。

Hope this makes sense.希望这是有道理的。

Please let me know if anyone has tried or can help me to achieve the same.请让我知道是否有人尝试过或可以帮助我实现相同的目标。 Thanks.谢谢。

You can scroll one cell at a time by setting pagingEnabled = YES .您可以通过设置pagingEnabled = YES一次滚动一个单元格。 Or for regular scrolling, you can adjust where the deceleration ends by overriding targetContentOffsetForProposedContentOffset:withScrollingVelocity: of UICollectionViewLayout .或者对于常规滚动,您可以通过覆盖targetContentOffsetForProposedContentOffset:withScrollingVelocity:来调整减速结束的UICollectionViewLayout When using the later method, you would typically do the following:使用后一种方法时,您通常会执行以下操作:

  1. Determine the nearest index path to proposedContentOffset .确定最近指数路径proposedContentOffset If there are no gaps between your cells, you can use [UICollectionView indexPathForItemAtPoint:] .如果您的单元格之间没有间隙,您可以使用[UICollectionView indexPathForItemAtPoint:] Otherwise, you may have to inspect your layout in some way to determine which index path you want to scroll to.否则,您可能必须以某种方式检查您的布局,以确定您想要滚动到哪个索引路径。
  2. Determine the frame of the index path you want to scroll to by getting the layout attributes for that index path通过获取该索引路径的布局属性来确定要滚动到的索引路径的框架
  3. Determine the content offset that will position the given frame where you want it.确定将给定框架定位到所需位置的内容偏移量。

Make sure your padding between cells is set to zero as well.确保单元格之间的填充也设置为零。 Paging enabled will stop on multiples of the view bounds.启用分页将在多个视图边界处停止。

    // Set up flow layout
            var flowLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout();
            flowLayout.minimumInteritemSpacing = 0
            flowLayout.minimumLineSpacing = 0

            collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: flowLayout)

            collectionView!.pagingEnabled = true

Check out my answer over here: ScrollView or CollectionView?在此处查看我的答案: ScrollView 或 CollectionView?

It sounds like you have paging enable but the page size doesn't match you cell size plus the insets left and right of the cell.听起来您启用了分页,但页面大小与单元格大小以及单元格左右两侧的插图不匹配。

斯威夫特 5:

collectionView.isPagingEnabled = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM