一个用于将任何 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, *)
。
要使用 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
。
import Refreshable
struct YourView: View {
var body: some View {
ScrollView {
LazyVStack {
...
}
.refreshable {
// do your work on refresh here
}
}
}
}
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
}
}
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 许可证下发布。