UICollectionView:用于分页的外屏自动滚动

我想通过下面的代码滚动我的应用程序中的UICollectionView

 int pages = ceil(aCollectionView.contentSize.height / aCollectionView.frame.size.height); for (int i = 0; i < pages; i ++) { NSArray *sortedVisibleItems = [[aCollectionView indexPathsForVisibleItems] sortedArrayUsingSelector:@selector(compare:)]; NSIndexPath *lastItem = [sortedVisibleItems lastObject]; // 2.next position NSInteger nextItem = lastItem.item + 1; NSInteger nextSection = lastItem.section; NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection]; [self takeImage]; dispatch_async(dispatch_get_main_queue(), ^ { [aCollectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO]; }); } 

并为每个页面的屏幕截图打印目的。 但它不滚动,总是多次打印第一页。

UICollectionView的属性

在这里输入图像说明

我是否错误或做错了方向?

你总是得到最后一个对象的NSIndexPath *lastItem = [sortedVisibleItems lastObject] ; 拍照。 这只会一直捕获相同的页面。

这是因为你没有从你的数组中移除lastObject。

通过使用删除您的lastObject

 [sortedVisibleItems removeLastObject]; 
 int pages = ceil(aCollectionView.contentSize.height / aCollectionView.frame.size.height); NSArray *visibleItems = [aCollectionView indexPathsForVisibleItems]; NSInteger row = 0; NSIndexPath *currentItem; for (NSIndexPath *indexPath in visibleItems) { if (row < indexPath.row){ row = indexPath.row; currentItem = indexPath; } } NSLog(@"current indexpath ; %ld",(long)currentItem.row); if (currentItem.row == pages-1) { return; } NSIndexPath *nextItem = [NSIndexPath indexPathForItem:currentItem.item + 1 inSection:currentItem.section]; [aCollectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES]; 

尝试这个

集合视图只在主线程上更新。 你的代码被封装在一个循环中,永远不允许主线程运行和更新。

那里有很多的讨论。 许多直接与UIScrollView做同样的事情,但它是同样的问题。

你可能想看看这个…不知道它是否符合你的需求,但我已经看到它多次引用: https : //github.com/sgr-ksmt/PDFGenerator

如果这不适合你,它可能有你需要的技术,所以稍微调查一下这个代码应该find你的答案。