InfinityScrollKit 包

InfinityScrollKit 是一个 SwiftUI 包,它简化了 iOS 列表中无限滚动的实现。该包处理用户滚动时加载更多项目,同时为普通单元格和最后一个加载/错误状态单元格提供可自定义的 UI。

特性

安装

Swift Package Manager

通过转到您的 Xcode 项目来添加包

  1. 在文件导航器中选择您的项目。
  2. 选择您要添加包的项目或目标。
  3. 转到“Package Dependencies”选项卡。
  4. 单击 + 按钮。
  5. 使用存储库 URL 搜索 InfinityScrollKit
    https://github.com/PierreJanineh-com/InfinityScrollKit
  6. 按照提示完成安装。

CocoaPods

  1. 如果您的项目中尚未使用 CocoaPods,请首先运行 sudo gem install cocoapods,然后运行 pod init。(有关安装 CocoaPods 的更多信息,点击此处。)

  2. 将以下内容添加到您的 Podfile(在 target 部分中)

    pod 'InfinityScrollKit'
  3. 运行 pod install

用法

请在此 repo 中查看完整示例。

基本用法

下面是一个基本用法示例,我们在其中创建一个具有自定义项目和最后一个单元格视图的无限滚动列表

import SwiftUI
import InfiniteScrollView

struct ContentView: View {
    @State private var items: [MyItem] = []
    
    var body: some View {
        InfiniteScrollView(
            arr: $items,
            cellView: { item in
                Text(item.name) // Customize the view for each item
            }
        )
    }
}

自定义

import SwiftUI
import InfinityScrollKit

struct ContentView: View {
    @State private var arr: [String] = []
    @State private var isLoading: Bool = false
    
    var body: some View {
        InfiniteScrollView(
            arr: $arr,
            options: options,
            onLoadingChanged: onLoadingChanged,
            cellView: CellView,
            lastCellView: LastCellView,
            emptyArrView: EmptyArrView
        )
        // You can also use ScrollView modifiers directly
        .scrollIndicators(.hidden)
    }

    private var options: Options<String> {
        .init(
            orientation: .horizontal,
            countPerPage: 2,
            paginationOptions: .init(
                onPageLoad: {
                    // Replace this with an API pagination request
                    try? await Task.sleep(nanoseconds: 5 * 1_000_000_000)
                    
                    var fetchedItems: [String]
                    // ...
                    // fetch additional items to add to the current array
                    
                    return fetchedItems
                },
                concatMode: .auto  //.auto for automatically adding pages to the array instead of passing the full array everytime.
            )
        )
    }

    private func onLoadingChanged(_ isLoading: Bool) {
        self.isLoading = isLoading
        if self.isLoading {
            // Do anything here...
        }
    }

    @ViewBuilder func CellView(_ item: String) -> some View {
        Text(item)
    }

    @ViewBuilder func LastCellView() -> some View {
        ProgressView()
            .padding()
    }

    @ViewBuilder func EmptyArrView() -> some View {
        Text("No items to display...")
    }
}

平台

InfiniteScrollView 包支持以下平台

贡献

欢迎通过创建 issue 或提交 pull request 来进行贡献。在提交之前,请确保

  1. Fork 该存储库。
  2. 创建您的 feature 分支 (git checkout -b feature/my-feature)
  3. 提交您的更改 (git commit -m 'Add some feature')
  4. 推送到该分支 (git push origin feature/my-feature)
  5. 打开一个 pull request。

许可

本项目根据 MIT 许可证获得许可。 有关更多详细信息,请参见 LICENSE 文件。