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

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

问题描述

我有一个可以一次显示大约 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?

推荐答案

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

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天全站免登陆