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

问题描述 投票:11回答:4

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

我尝试过使用:

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

和:

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

他们工作但他们暂时“白出”当前的Cell以呈现nextCell,而刷卡在转换过程中显示两个单元格。

理想情况下我想使用:

- (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,并且没有动画....

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

ios objective-c uiscrollview uicollectionview uicollectionviewcell
4个回答
40
投票

假设你的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];

6
投票

这是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)

6
投票

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

@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)
}

1
投票

Swift 4.1回答

    let visibleItems = self.collectionView.indexPathsForVisibleItems
    let currentItem = visibleItems.first
    let nextRow = (currentItem?.row)! + visibleItems.count
    if nextRow < elementArray.count {
        let nextIndexPath = IndexPath.init(row: nextRow, section: (currentItem?.section)!)
        self.collectionView.scrollToItem(at: nextIndexPath, at: UICollectionViewScrollPosition.left, animated: true)
    } else {
        let nextIndexPath = IndexPath.init(row: 0, section: (currentItem?.section)!)
        self.collectionView.scrollToItem(at: nextIndexPath, at: UICollectionViewScrollPosition.right, animated: true)
    }

根据您的要求更改滚动位置以及您要跳过的行数(这里我跳过了visibleItems.count

© www.soinside.com 2019 - 2024. All rights reserved.