带分页的UICollectionView - 设置页面宽度 [英] UICollectionView with paging - setting page width

查看:499
本文介绍了带分页的UICollectionView - 设置页面宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Collection View,一次可以显示大约3.5个单元格,我希望它能够启用分页。但我希望它能够捕捉到每个单元格(就像App Store应用程序一样),而不是滚动视图的整个宽度。我该怎么做?

I have a Collection View that can show about 3.5 cells at a time, and I want it to be paging-enabled. But I'd like it to snap to each cell (just like the App Store app does), and not scroll the full width of the view. How can I do that?

推荐答案

您可以通过成为集合视图的委托并实现方法来捕捉单元格: / p>

You can snap to cells by being the delegate of the collection view and implementing the method:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset

这告诉你用户已完成拖动,它允许你修改 targetContentOffset 与您的单元格对齐(即舍入到最近的单元格)。请注意,您需要注意如何修改targetContentOffset;特别是,你需要避免更改它,以便视图需要在传递速度的相反方向滚动,否则你会得到动画故障。如果你谷歌获取该方法名称,你可以找到很多这方面的例子。

This tells you that the user has finished a drag and it allows you to modify the targetContentOffset to align with your cells (i.e. round to the nearest cell). Note that you need to be careful about how you modify the targetContentOffset; in particular, you need to avoid changing it so that the view needs to scroll in the opposite direction of the passed velocity, or you'll get animation glitches. You can probably find many examples of this if you google for that method name.

这篇关于带分页的UICollectionView - 设置页面宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆