UIRefreshKit 2.0.0

UIRefreshKit 2.0.0

Egor Korotkii维护。



  • 作者
  • Egor Korotkii

UIRefreshKit

Languages CocoaPods Compatible Swift Package Manager License MIT

UIRefreshKit 是一个自定义库,为 UICollectionViewUITableView 提供可定制的下拉刷新和自动分页功能。

功能

  • 可定制下拉刷新
  • 自动分页
  • 易于集成

安装

Swift 包管理器 (SPM)

要使用 SPM 安装 UIRefreshKit,请将以下内容添加到您的 Package.swift 文件中

dependencies: [
    .package(url: "https://github.com/atsed/UIRefreshKit.git", from: "2.0.0")
]

然后,在目标依赖关系部分,包括 UIRefreshKit

.target(
    name: "YourTargetName",
    dependencies: ["UIRefreshKit"]
)

CocoaPods

要使用 CocoaPods 安装 UIRefreshKit,请将以下内容添加到您的 Podfile

pod 'UIRefreshKit', '~> 2.0.0'

然后运行

pod install

使用方法

下拉刷新

查看下拉刷新动画

要将 PullToRefresh 添加到屏幕,您需要为您的表格/集合视图设置 pullToRefresh 属性。您可以使用 RefreshControl 来做这件事,或者创建自己的

collectionView.pullToRefresh = RefreshControl()

PullToRefresh 触发时要执行的操作被包装在一个区块中

collectionView.pullToRefreshAction = {
    // Your action
}

要检查表格/集合视图中是否正在发生下拉刷新动画,您可以使用 isPullToRefreshing 属性

if collectionView.isPullToRefreshing {
    // Do something
}

要结束下拉刷新动画,请调用 endPullToRefresh 方法

collectionView.endPullToRefresh()

如果您需要从顶部添加一个静态内边距,使得下拉刷新动画从该位置开始,请使用 setPullToRefreshStaticInsetTop 方法

collectionView.setPullToRefreshStaticInsetTop(value: 10.0)

自动分页

查看分页动画

要将自动分页添加到屏幕,您需要为您的表格/集合视图设置 paginationRefresh 属性。您可以使用 RefreshControl 来做这件事,或者创建自己的

collectionView.paginationRefresh = RefreshControl()

当自动分页被触发时,要执行的操作被包装在一个区块中

collectionView.paginationRefreshAction = {
    // Your action
}

要检查表格/集合视图中是否正在发生自动分页动画,您可以使用 isPaginationRefreshing 属性

if collectionView.isPaginationRefreshing {
    // Do something
}

如果您需要禁用自动分页,例如在出现错误或加载最后一页时,请调用 disablePaginationRefresh 方法

collectionView.disablePaginationRefresh()

要恢复自动分页,例如在错误恢复后,请调用 reloadPaginationRefresh 方法

collectionView.reloadPaginationRefresh()

创建自定义 RefreshControl

要创建自定义的 RefreshControl,您需要从 BaseRefreshControl 继承并实现所需的方法

class CustomRefreshControl: BaseRefreshControl {
    override func setProgress(to newProgress: CGFloat) {
        // Update the appearance of your custom refresh control based on the progress
    }

    override func startRefreshing() {
        // Start the refreshing animation and the haptic
    }

    override func endRefreshing() {
        // Stop the refreshing animation
    }

    override func resume() {
        // Resume or complete the animation based on the current state
    }
}

示例

以下是在视图控制器中使用 UIRefreshKit 的基本示例

import UIKit
import UIRefreshKit

class ViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.pullToRefresh = RefreshControl()
        collectionView.pullToRefreshAction = { [weak self] in
            // PullToRefresh action
            DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                self?.collectionView.endPullToRefresh()
            }
        }

        collectionView.paginationRefresh = RefreshControl(size: .small, isHapticEnabled: false)
        collectionView.paginationRefreshAction = { [weak self] in
            // Pagination action
            DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                self?.collectionView.reloadPaginationRefresh()
            }
        }
    }
}

许可证

UIRefreshKit 可在 MIT 许可证下获得。有关更多信息,请参阅 LICENSE 文件。