水平 UIcollectionView 从右向左滚动

horizontal UIcollectionView Scroll start from right to left

我确实希望视图在加载时先显示正确的单元格,然后我应该从那里向左滚动我尝试了这段代码,但没有成功

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! customCell
        cell.data = self.data[indexPath.row]
        self.collectionView.scrollToItem(at: IndexPath(item: self.data.count, section: 0) as IndexPath, at: .right, animated: false)
        return cell
    }

在此处为这张照片点赞当视图加载时它会显示第一个右侧单元格

这不是 UICollection 视图的常见行为,但是旋转和翻转您的 collectionView 怎么样,那么最后一个单元格将是您的第一个 ;)。

如果感兴趣,请查看 CGAffineTransform(Rotation angle)CGAffineTransform(TranslationY)

由于 UICollectionView 从右向左水平滚动,您可以在 collection 视图出现时将其设置为滚动到最右侧!这样用户就可以开始从右向左滚动

YourCollectionView 是您想要的 CollectionView 的名称

YourObjectListData 是 collection 视图的数据源

self.YourCollectionView.reloadData()
self.YourCollectionView.scrollToItem(at: NSIndexPath(item: self.YourObjectListData.count - 1, section: 0) as IndexPath, at: .right, animated: false)