如何在IOS中以编程方式滚动UICollectionViewCell?

我有一个垂直UICollectionView与每个单元格占用整个self.view.frame我可以轻松地向上滑动页面到下一个单元格,但我想按button做同样的事情。

我试过使用:

 - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated 

和:

 - (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated 

他们的工作,但他们暂时“消灭”当前细胞呈现下一个细胞,而滑动显示在过渡期间的两个单元格。

理想情况下,我想使用:

 - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated 

但我不知道如何访问nextCell的indexPath …我试过了:

 NSIndexPath *nextIndexPath = [_collectionView indexPathForItemAtPoint:CGPointMake(25, (_collectionView.frame.size.height-25)*(_currentIndexRowForCellOnScreen+1))]; 

其中_currentIndexRowForCellOnScreenindexPath.row在屏幕上首次出现的UICollectionView

 - (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

但是当我把它放入:

 - (NSIndexPath *)indexPathForCell:(UICollectionViewCell *)cell 

它在第一个Cell之后返回NULL,并且不animation….

任何方向将不胜感激。 感谢您的时间。

假设你的collectionView只包含1个部分,并且假定每个项目占据了整个框架,那么你可以做这样的事情;

  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]; 

这里是Swift版本

Swift 2.x:

 let visibleItems: NSArray = self.collectionView.indexPathsForVisibleItems() let currentItem: NSIndexPath = visibleItems.objectAtIndex(0) as! NSIndexPath let nextItem: NSIndexPath = NSIndexPath(forRow: currentItem.item + 1, inSection: 0) self.collectionView.scrollToItemAtIndexPath(nextItem, atScrollPosition: .Top, animated: true) 

Swift 3.x

 let visibleItems: NSArray = self.collectionView.indexPathsForVisibleItems as NSArray let currentItem: IndexPath = visibleItems.object(at: 0) as! IndexPath let nextItem: IndexPath = IndexPath(item: currentItem.item + 1, section: 0) self.collectionView.scrollToItem(at: nextItem, at: .top, animated: true) 

到目前为止,这是我遇到的最好的方法,诀窍是滚动到包含下一个对象的框架。 请参阅代码

 @IBAction func actionPreviousFriends(_ sender: Any) { let collectionBounds = self.collectionView.bounds let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x - collectionBounds.size.width)) self.moveToFrame(contentOffset: contentOffset) } /* -------------- display next friends action ----------------*/ @IBAction func actionNextFriends(_ sender: Any) { let collectionBounds = self.collectionView.bounds let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x + collectionBounds.size.width)) self.moveToFrame(contentOffset: contentOffset) } func moveToFrame(contentOffset : CGFloat) { let frame: CGRect = CGRect(x : contentOffset ,y : self.collectionView.contentOffset.y ,width : self.collectionView.frame.width,height : self.collectionView.frame.height) self.collectionView.scrollRectToVisible(frame, animated: true) }