简体   繁体   English

无休止的UICollectionView幻灯片+ scrollToItemAtIndexPath

[英]Endless UICollectionView Slideshow + scrollToItemAtIndexPath

We have a UICollectionView that fills the entire screen to mimic a full-screen photo slideshow. 我们有一个UICollectionView可以填充整个屏幕,以模拟全屏照片幻灯片显示。 The collection view's data is backed by a NSFetchedResultsController. 集合视图的数据由NSFetchedResultsController支持。 Only one cell of the collection view displays on the screen at any given time. 在任何给定时间,屏幕上仅显示一个集合视图的单元格。 There is also a repeatable NSTimer set at an interval of 5 seconds that fires off a method "gotoNextPage" which calls scrollToItemAtIndexPath to the next fetched object (the next UICollectionView cell, the next slideshow photo). 还有一个以5秒为间隔设置的可重复NSTimer,它触发了方法“ gotoNextPage”,该方法调用scrollToItemAtIndexPath到下一个获取的对象(下一个UICollectionView单元格,下一个幻灯片照片)。 This is a smooth transition forward. 这是一个平稳的过渡。 When we get to the end of the slideshow we would like to seamlessly advance forward to the first photo and start the slideshow over again. 当我们到达幻灯片的结尾时,我们想无缝地前进到第一张照片,然后重新开始幻灯片。 Of course when we scrollToItemAtIndexPath back to the first photo it scrolls backwards rapidly (depending on how many fetched objects we have). 当然,当我们将scrollToItemAtIndexPath返回到第一张照片时,它会快速向后滚动(取决于我们拥有多少个获取的对象)。 We want the forward animation to be consistent no matter if you are at the beginning, middle, or end of the slideshow/UICollectionView. 无论您位于幻灯片/ UICollectionView的开始,中间还是结尾,我们都希望前向动画保持一致。

Does anyone have a creative way to solve this problem? 有没有人有创造性的方法来解决这个问题?

Thanks 谢谢

You could just let the index path item number increase continuously, and base your cellForItemAtIndexPath and other table logic on the remainder after dividing by the photo count. 您可以让索引路径项号连续增加,然后在除以照片计数之后,将cellForItemAtIndexPath和其他表逻辑基于其余部分。 So you could code: 因此,您可以编写以下代码:

NSUInteger photoCount  = [[self.fetchedResultsController fetchedObjects] count];
NSIndexPath *fetchIndexPath = [NSIndexPath indexPathForItem:(indexPath.item % photoCount) inSection:0];
Photo *myPhoto = [self.fetchedResultsController objectAtIndexPath:fetchIndexPath];

A similar approach should work in the other tableView/NSFetchedResultsController datasource/delegate methods. 其他tableView / NSFetchedResultsController数据源/委托方法中也应使用类似的方法。 EDIT As you note in your comment, you would have to set numberOfItemsInSection to a very large number (NSIntegerMax?). 编辑正如您在评论中指出的那样,您必须将numberOfItemsInSection设置为一个非常大的数字(NSIntegerMax?)。

Alternatively... You could amend your cellForItemAtIndexPath so that there is an extra cell, after the final photo, which contains the same photo as for row 0. 或者...您可以修改cellForItemAtIndexPath以便在最终照片之后有一个额外的单元格,其中包含与第0行相同的照片。

Photo *myPhoto
NSUInteger photoCount  = [[self.fetchedResultsController fetchedObjects] count];
if (indexPath.item = photoCount) {
    myPhoto = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
} else {
    myPhoto = [self.fetchedResultsController objectAtIndexPath:indexPath];
}
... configure cell...

You would have to amend numberOfItemsInSection to return photoCount+1 . 您必须修改numberOfItemsInSection才能返回photoCount+1

After you scroll forward to this new final cell, scroll immediately to the first cell but without animation . 向前滚动到这个新的最终单元格后,请立即滚动到第一个单元格, 但不设置动画 Since the first and last cell have the same photo, the user will not see any transition. 由于第一个和最后一个单元格具有相同的照片,因此用户将看不到任何过渡。 Then you can revert to scrolling forward with animation. 然后,您可以还原为向前滚动动画。

if (currentItem == photoCount -1) {
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:photoCount inSection:0] atScrollPosition:<your preference> animated:NO];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:<your preference> animated:YES];
} else {
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:(currentItem+1) inSection:0] atScrollPosition:<your preference> animated:YES];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UICollectionView scrollToItemAtIndexPath - UICollectionView scrollToItemAtIndexPath UICollectionView-分隔scrollViewDidScroll和scrollToItemAtIndexPath - UICollectionView - separating scrollViewDidScroll and scrollToItemAtIndexPath UICollectionView水平滚动scrollToItemAtIndexPath之后: - UICollectionView Horizontal Scroll after scrollToItemAtIndexPath: UICollectionView.scrollToItemAtIndexPath如何工作? - How UICollectionView.scrollToItemAtIndexPath works? UICollectionView scrollToItemAtIndexPath不起作用 - UICollectionView scrollToItemAtIndexPath doesn't work 如果在viewWillAppear中调用scrollToItemAtIndexPath,则在UICollectionView上未更新contentOffset - contentOffset not updated on UICollectionView if scrollToItemAtIndexPath is called inside viewWillAppear 使用UICollectionView scrollToItemAtIndexPath返回到上一个选定的单元格 - Using UICollectionView scrollToItemAtIndexPath to return to last selected cell UIcollectionView scrollToitemAtIndexPath 不适用于不可见的单元格 - UIcollectionView scrollToitemAtIndexPath does not work for non visible cells 在scrollToItemAtIndexPath之后获取UICollectionView单元格位置 - Get UICollectionView cell position after scrollToItemAtIndexPath UICollectionView scrollToItemAtIndexPath在导航控制器中无法正常运行 - UICollectionView scrollToItemAtIndexPath not functioning properly in navigation controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM