RefreshControlKit
RefreshControlKit 是一个可像 UIRefreshControl 使用的自定义 RefreshControl 库。
要求
- Swift 5 或更高版本
- iOS 12 或更高版本
用法
RefreshControl
可以通过使用 @RefreshControlling
的属性包装器来使用。
import UIKit
import RefreshControlKit
class ViewController: UIViewController {
@RefreshControlling(wrappedValue: nil, view: CustomView())
@IBOutlet private var myScrollingView: UIScrollView!
// or
@RefreshControlling(view: CustomView())
private var myScrollingView = UIScrollView()
由于 项目值 是 RefreshControl
,您可以通过以下方式像 UIRefreshControl 一样使用它。
func configureRefreshControl () {
$myScrollingView.addTarget(self, action:
#selector(handleRefreshControl),
for: .valueChanged)
}
@objc func handleRefreshControl() {
// Update your content…
// Dismiss the refresh control.
DispatchQueue.main.async {
self.$myScrollingView.endRefreshing()
}
}
您还可以直接使用 RefreshControl
。
let refreshControl = RefreshControl(view: CustomView())
func configureRefreshControl () {
myScrollingView.addRefreshControl(refreshControl)
refreshControl.addTarget(self, action:
#selector(handleRefreshControl),
for: .valueChanged))
}
您还可以通过编程方式启动刷新。
refreshControl.beginRefreshing()
创建自定义 RefreshControlView
RefreshControl
的高度取决于自定义视图的高度。
要创建自定义视图,必须满足以下条件:
- 是
UIView
的子类 - 符合
RefreshControlView
class CustomView: UIView, RefreshControlView {
func willRefresh() {
// Something to do before refreshing.
}
func didRefresh() {
// Something to do after refreshing.
}
func didScroll(_ progress: RefreshControl.Progress) {
// Something to do while scrolling.
// `Progress` expresses the progress to the height of the trigger as 0.0 to 1.0.
}
}
请参阅示例代码以获取详细信息。
配置
public struct Configuration {
public var layout: Layout
public var trigger: Trigger
}
布局
Layout
可以选择为 top
或 bottom
。
public enum Layout {
/// The top of the `RefreshControl` is anchored at the top of the `ScrollView` frame.
case top
/// The bottom of the `RefreshControl` is anchored above the content of the `ScrollView`.
case bottom
}
@RefreshControlling(view: CustomView(), configuration: .init(layout: .bottom))
private var myScrollingView = UIScrollView()
top | bottom |
---|---|
![]() |
![]() |
触发
高度
@RefreshControlling(view: CustomView(), configuration: .init(trigger: .init(height: 50)))
private var myScrollingView = UIScrollView()
您可以指定刷新开始的高度。默认为自定义视图的高度。
事件
事件
可以选择为拖拽
或释放
。
public enum Event {
/// When it is pulled to the trigger height, `beginRefreshing` is called.
case dragging
/// If the height of the trigger is exceeded at the time of release, `beginRefreshing` is called.
case released
}
@RefreshControlling(view: CustomView(), configuration: .init(trigger: .init(event: .released)))
private var myScrollingView = UIScrollView()
拖拽 | 释放 |
---|---|
![]() |
![]() |
安装
SwiftPM
将RefreshControlKit添加为依赖
import PackageDescription
let package = Package(
name: "YourApp",
dependencies: [
.Package(url: "https://github.com/hirotakan/RefreshControlKit.git", majorVersion: 0),
]
)
CocoaPods
在你的Podfile中添加以下内容
pod 'RefreshControlKit'
Carthage
在你的Cartfile中添加以下内容
github "hirotakan/RefreshControlKit"
许可证
RefreshControlKit是在MIT许可证下发布的。有关详细信息,请参阅LICENSE。