{"id":19302260,"url":"https://github.com/apestalk/atpagingbycell","last_synced_at":"2025-09-04T17:36:23.062Z","repository":{"id":86574555,"uuid":"279232370","full_name":"ApesTalk/ATPagingByCell","owner":"ApesTalk","description":"A example of perfectly paging by smaller cell (only one cell at a time) not by screen, which like AppStore's banner and Alipay's banner. 一个使用UICollectionView实现非全屏cell的paging效果，就像AppStore游戏tab顶部banner和支付宝财富tab的财富直通车banner一样的效果（主要是非全屏cell的情况下实现不管是慢速拖拽还是快速拖拽都每次只轮播一个cell）。","archived":false,"fork":false,"pushed_at":"2020-07-19T08:06:46.000Z","size":2024,"stargazers_count":14,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-01T22:37:11.643Z","etag":null,"topics":["paging","paging-by-cell","pagingenabled","uicollectionview","uiscrollview"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ApesTalk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-13T06:41:51.000Z","updated_at":"2023-09-13T07:29:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"616632e2-0351-4a0e-b423-ace22824d109","html_url":"https://github.com/ApesTalk/ATPagingByCell","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ApesTalk%2FATPagingByCell","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ApesTalk%2FATPagingByCell/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ApesTalk%2FATPagingByCell/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ApesTalk%2FATPagingByCell/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ApesTalk","download_url":"https://codeload.github.com/ApesTalk/ATPagingByCell/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250221414,"owners_count":21394700,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["paging","paging-by-cell","pagingenabled","uicollectionview","uiscrollview"],"created_at":"2024-11-09T23:21:04.794Z","updated_at":"2025-04-22T10:33:41.633Z","avatar_url":"https://github.com/ApesTalk.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ATPagingByCell\n\nA example of perfectly paging by smaller cell (only one cell at a time) not by screen, which like AppStore's banner and Alipay's banner. \n一个使用UICollectionView实现非全屏cell的paging效果，就像AppStore游戏tab顶部banner和支付宝财富tab的财富直通车banner一样的效果（主要是非全屏cell的情况下实现不管是慢速拖拽还是快速拖拽都每次只轮播一个cell）。\n\n借助UICollectionView的pagingenabled属性，我们能快速做出漂亮的滑动翻页paging效果，可以满足我们引导页、图片预览、banner等等轮播需求。但当我们的cell宽度不是占满整个UICollectionView的时候，比如我们经常遇到一屏要展示三个cell的效果，而且还要实现一次滑动一个cell的效果。这时候，我们不得不实现``- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset``方法或者自定义UICollectionViewFlowLayout实现``- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity``通过矫正其偏移量来将距离UICollectionView中心点最近的cell居中显示。\n\n但是，很显然，单纯这样处理一下，在慢慢拖拽滑动的情况下不会有问题，在快速滑动时会跨cell，也就是一个假的paging效果，如下图所示：\n\n![伪paging by cell](https://github.com/ApesTalk/ATPagingByCell/blob/master/emoji0.gif)\n\n\n如果我们仔细看AppStore的游戏tab顶部banner或者支付宝财富tab的财富直通车banner的paging效果，会发现它们才是真正的paging效果，不管你怎么快速滑动，它每次就是只滑动一个cell，非常的完美。那他们是怎么实现的，为毛我们处理不了这种细节问题？？？再看看我们目前遇到的问题，只要我们限制不让其每次滑动跨cell就行了，不多解释了，关键代码如下：\n\n\n```\n//paging by cell | paging with one cell at a time\n- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView\n{\n    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout;\n    CGFloat cellWidth = [UIScreen mainScreen].bounds.size.width - layout.sectionInset.left - layout.sectionInset.right;\n    CGFloat cellPadding = layout.minimumLineSpacing;\n    self.currentPage = (scrollView.contentOffset.x - cellWidth / 2) / (cellWidth + cellPadding) + 1;\n}\n\n- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset\n{\n    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout;\n    CGFloat cellWidth = [UIScreen mainScreen].bounds.size.width - layout.sectionInset.left - layout.sectionInset.right;\n    CGFloat cellPadding = layout.minimumLineSpacing;\n    NSInteger page = (scrollView.contentOffset.x - cellWidth / 2) / (cellWidth + cellPadding) + 1;\n\n    if (velocity.x \u003e 0) page++;\n    if (velocity.x \u003c 0) page--;\n    page = MAX(page, 0);\n    \n    //!!!!:此处注掉：会导致快速滑动会跨越多个cell\n    NSInteger prePage = self.currentPage - 1;\n    if(prePage \u003e 0 \u0026\u0026 page \u003c prePage){\n        page = self.currentPage - 1;\n    } else if (page \u003e self.currentPage + 1){\n        page = self.currentPage + 1;\n    }\n    \n    self.currentPage = page;\n    \n    CGFloat newOffset = page * (cellWidth + cellPadding);\n    targetContentOffset-\u003ex = newOffset;\n}\n```\n\n最终效果如下：\n\n![like AppStore and Alipay](https://github.com/ApesTalk/ATPagingByCell/blob/master/emoji1.gif)\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapestalk%2Fatpagingbycell","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapestalk%2Fatpagingbycell","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapestalk%2Fatpagingbycell/lists"}