如何在 iOS Objective-c 的 UICollectionView 中创建无限滚动 [英] How can I create Infinite Scrolling in UICollectionView for iOS Objective-c

查看:18
本文介绍了如何在 iOS Objective-c 的 UICollectionView 中创建无限滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的 UICollectionView 中创建无限滚动?

How To Create Infinite Scrolling in my UICollectionView?

附上截图:

现在我的 NSArray

NSArray* nameArr = [NSArray arrayWithObjects: @"0", @"1", @"2", @"3", @"4", nil];

我想在collectionView中创建一个Infinite Scrolling After Complete数组一次又一次地重复所有单元格.

无限滚动需要左右两边.

如果是,那我该如何实现,请在objective-c中给出参考.

If yes, then how can I implement please give a reference in objective-c.

谢谢!!!提前

推荐答案

简单的方法是使用 StableCollectionViewLayout.

实现的基本原理是创建 UICollectionViewLayout 的子类并覆盖这些方法

The basic principle of implementation is creating a subclass of UICollectionViewLayout and overriding these methods

override open func prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem]) {
    super.prepare(forCollectionViewUpdates: updateItems)

    // there is possible to calculate a content offset the difference
    // with the help layout attributes for each updated item or only visible items
    self.offset = calculate(...)
}

override open func finalizeCollectionViewUpdates() {
    super.finalizeCollectionViewUpdates()
    self.offset = nil
}

override open func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
    // there is necessary to add difference to/instead proposedContentOffset
    if let offset = self.offset {
       return offset
    }
    return proposedContentOffset
}

它允许在插入或删除发生时保持内容偏移.然后你可以在头部插入并删除尾部,反之亦然.

It allows keeping content offset when inserting or deleting happens. Then you can just insert at the head and delete the tail, or vice versa.

这篇关于如何在 iOS Objective-c 的 UICollectionView 中创建无限滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆