UICollectionView自动滚动分页

编程入门 行业动态 更新时间:2024-10-28 00:24:39
本文介绍了UICollectionView自动滚动分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的项目有一个UIcollectionView.此水平分页并具有四个对象.我想要我的收藏查看自动滚动分页.哪种使用方法?

my project have a UIcollectionView. This Horizontal paging and have four object. i’m want my collectionView Auto Scroll Paging. which use methods?

推荐答案

至于理论,我只是解释关键点.例如,如果您想在某个位置自动循环显示5张图像,就像广告栏一样.您可以创建一个包含100个部分的UICollectionView,每个部分有5个项目.创建UICollectionView后,将其原始位置设置为第50个第0个项目(MaxSections的中间) ),以便您可以向左或向右滚动.不必担心它会结束,因为在计时器运行时,我已将位置重置为与MaxSections的中间位置相等.永远不要跟我争论:当用户继续滚动时,它也将结束".如果发现只有5张图像,他会继续滚动吗?我相信这个世界上很少有如此愚蠢的用户!

As for theory, I just explain the key point. For example, if you want to auto-cycle display 5 images in somewhere, just as ads bar. You can create a UICollectionView with 100 sections, and there're 5 items in every section. After creating UICollectionView, set its original position is equal to the 50th section 0th item(middle of MaxSections ) so that you can scroll to left or right. Don't worry it will ends, because I have reset the position equal to middle of MaxSections when the Timer runs. Never argue with me :" it also will ends when the user keep scrolling by himself" If one find there're only 5 images, will he keep scrolling!? I believe there is few such silly user in this world!

注意:在以下代码中,headerView是UICollectionView,headerNews是数据.我建议将MaxSections设置为100.

Note: In the following codes,the headerView is UICollectionView,headerNews is data. And I suggest set MaxSections = 100.

在自定义collectionView之后添加NSTimer(确保首先正确设置UICollectionViewFlowLayout):

Add a NSTimer after custom collectionView(Ensure the UICollectionViewFlowLayout is properly set at first):

- (void)addTimer { NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(nextPage) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; self.timer = timer; }

然后实现@selector(nextPage):

then implement @selector(nextPage):

- (void)nextPage { // 1.back to the middle of sections NSIndexPath *currentIndexPathReset = [self resetIndexPath]; // 2.next position NSInteger nextItem = currentIndexPathReset.item + 1; NSInteger nextSection = currentIndexPathReset.section; if (nextItem == self.headerNews.count) { nextItem = 0; nextSection++; } NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection]; // 3.scroll to next position [self.headerView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES]; }

最后实现resetIndexPath方法:

last implement resetIndexPath method:

- (NSIndexPath *)resetIndexPath { // currentIndexPath NSIndexPath *currentIndexPath = [[self.headerView indexPathsForVisibleItems] lastObject]; // back to the middle of sections NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:MaxSections / 2]; [self.headerView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO]; return currentIndexPathReset; }

为了控制NSTimer,您必须实现一些其他方法和UIScrollView的委托方法:

In order to control NSTimer , you have to implement some other methods and UIScrollView's delegate methods:

- (void)removeTimer { // stop NSTimer [self.timer invalidate]; // clear NSTimer self.timer = nil; } // UIScrollView' delegate method - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [self removeTimer]; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { [self addTimer]; }

更多推荐

UICollectionView自动滚动分页

本文发布于:2023-11-26 03:41:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632493.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   UICollectionView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!