可刷新 (Refreshable)

Latest release

contact: @lexkraev Telegram Group

一个用于将任何 SwiftUI 视图标记为可刷新的库,类似于 Apple 的 refreshable(action:),该功能从 iOS 15 开始可用,并且仅在 List 中可用 🤷🏼‍♂️。

你可以将 Refreshable 与任何视图一起使用 (例如 LazyVStack)。

👨🏻‍💻 欢迎订阅 Telegram 频道:SwiftUI dev

要求

基本上,将此软件包添加到低于 iOS 14 的最低版本开发的项目中没有任何限制。

但是你可以从 iOS 14 开始将 Refreshable 与视图一起使用。

使用 @available(iOS 14.0, *)if #available(iOS 14, *)

安装

Swift Package Manager

要使用 SwiftPM 将 Refreshable 集成到你的项目中,请将以下内容添加到你的 Package.swift 文件中。

dependencies: [
    .package(url: "https://github.com/c-villain/Refreshable", from: "0.1.0"),
],

或通过 XcodeGen 插入到你的 project.yml 文件中。

name: YourProjectName
options:
  deploymentTarget:
    iOS: 12.0
packages:
  Refreshable:
    url: https://github.com/c-villain/Refreshable
    from: 0.1.0
targets:
  YourTarget:
    type: application
    ...
    dependencies:
       - package: Refreshable

快速开始

基本上有两种方法可以将你的视图标记为 Refreshable

最简单的方法是将 modifier refreshable 添加到你的视图中。
import Refreshable

struct YourView: View {
    
    var body: some View {
        ScrollView {
            LazyVStack {
                ...
            }
            .refreshable {
                // do your work on refresh here
            }
        }
    }
}
第二种方法是将 RefreshControl 手动添加到你的视图实现中。 不要忘记命名你视图的坐标空间!
import Refreshable

struct YourView: View {
    
    var body: some View {
        ScrollView {
            RefreshControl(coordinateSpace: .named("List")) { // <= HERE
                // do your work on refresh here
            }
            
            LazyVStack {
                ...
            }
        }
        .coordinateSpace(name: "List") // <= DON'T FORGET
    }
}
要控制可刷新视图在滚动期间的响应性,请使用 ScrollDistance。 或者使用默认值 😊。
import Refreshable

struct Constants {
    public static let isPad: Bool = UIDevice.current.userInterfaceIdiom == .pad
}

struct YourView: View {
    
    var body: some View {
        ScrollView {
            RefreshControl(Constants.isPad ? .long : .short, // <= HERE
                           coordinateSpace: .named("List")) { 
                // do your work on refresh here
            }
            
            LazyVStack {
                ...
            }
        }
        .coordinateSpace(name: "List")
    }
}

交流

许可证

Refreshable 软件包在 MIT 许可证下发布。