我的项目有一个 UIcollectionView。这种水平分页有四个对象。我想要我的collectionView自动滚动分页。哪些使用方法?
关于理论,我只是解释一下重点。例如,如果你想在某个地方自动循环显示5张图片,就像广告栏一样。您可以创建一个包含 100 个部分的 UICollectionView,每个部分有 5 个项目。创建UICollectionView后,设置其原始位置等于第50节第0项(MaxSections的中间) )以便您可以向左或向右滚动。不要担心它会结束,因为当计时器运行时,我已经将位置重置为等于 MaxSections 的中间。不要和我争论:“当用户继续自己滚动时,它也会结束”如果发现只有 5 个图像,他还会继续滚动吗?我相信这个世界上没有几个这么傻的用户!
注意:以下代码中,headerView为UICollectionView,headerNews为data。我建议设置 MaxSections = 100。
在自定义collectionView后添加一个NSTimer(确保首先正确设置UICollectionViewFlowLayout):
- (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):
- (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方法:
- (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 的委托方法:
- (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];
}
@Elijah_Lam 的 Swift 2.0 版本的回答:
func addTimer()
{
timer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "nextPage", userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
}
func nextPage()
{
var ar = cvImg.indexPathsForVisibleItems()
///indexPathsForVisibleItems()'s result is not sorted. Sort it by myself...
ar = ar.sort{if($0.section < $1.section)
{
return true
}
else if($0.section > $1.section)
{
return false
}
///in same section, compare the item
else if($0.item < $1.item)
{
return true
}
return false
}
var currentIndexPathReset = ar[1]
if(currentIndexPathReset.section > MaxSections)
{
currentIndexPathReset = NSIndexPath(forItem:0, inSection:0)
}
cvImg.scrollToItemAtIndexPath(currentIndexPathReset, atScrollPosition: UICollectionViewScrollPosition.Left, animated: true)
}
func removeTimer()
{
// stop NSTimer
timer.invalidate()
}
// UIScrollView' delegate method
func scrollViewWillBeginDragging(scrollView: UIScrollView)
{
removeTimer()
}
func scrollViewDidEndDragging(scrollView: UIScrollView,
willDecelerate decelerate: Bool)
{
addTimer()
}