如何在IOS中以编程方式滚动UICollectionViewCell? [英] How to scroll UICollectionViewCell programmatically in IOS?
问题描述
我有一个垂直的 UICollectionView
,每个单元格都占据了整个 self.view.frame
,我可以轻松地向上滑动以翻页到下一个单元格,但是我想只需按一下按钮即可完成相同的操作.
I have a vertical UICollectionView
with each cell taking up the entire self.view.frame
I can easily swipe upwards to page to the next cell, but I would like to do the same thing with the press of a button.
我尝试使用:
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
并且:
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
它们可以工作,但是它们会暂时变白"当前单元格以显示下一个单元格,而在切换过程中,刷卡会显示两个单元格.
They work but they temporarily "White-Out" the currentCell to present the nextCell, while the swiping shows both cells during the transition.
理想情况下,我想使用:
Ideally I would like to use:
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated
但是我不知道如何访问nextCell的 indexPath
...我已经尝试过:
But I don't know how to access the nextCell's indexPath
...
I've tried:
NSIndexPath *nextIndexPath = [_collectionView indexPathForItemAtPoint:CGPointMake(25, (_collectionView.frame.size.height-25)*(_currentIndexRowForCellOnScreen+1))];
其中 _currentIndexRowForCellOnScreen
是 UICollectionView
在屏幕上的首次出现的 indexPath.row
:
Where _currentIndexRowForCellOnScreen
is the indexPath.row
of the UICollectionView
's first appearance on screen in:
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
但是当我放入它时:
- (NSIndexPath *)indexPathForCell:(UICollectionViewCell *)cell
它在第一个Cell之后返回NULL,并且不设置动画....
it returns NULL after the first Cell, and does not animate....
任何方向将不胜感激.谢谢您的时间.
Any direction would be greatly appreciated. Thank you for your time.
推荐答案
假设您的collectionView仅包含1个部分,并且鉴于每个项目都占据了整个框架,那么您可以执行以下操作;
Assuming your collectionView contains only 1 section, and given that each item occupies the whole frame then you could do something like this;
NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems];
NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
NSIndexPath *nextItem = [NSIndexPath indexPathForItem:currentItem.item + 1 inSection:currentItem.section];
[self.collectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
这篇关于如何在IOS中以编程方式滚动UICollectionViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!