Progress
除了任何种类的全屏捕获并锁定用户交互的ProgresHUDs,Progress还提供了一个更精确、更优雅的进度指示器。Progress允许您仅用一行代码将任意数量的进度视图添加到每个视图中。进度指示器可以是任何内置/自定义进度器的任意组合。Progress帮助您在iOS应用程序中提供流畅的用户体验。
示例
要运行示例项目,请克隆仓库,然后首先从示例目录运行pod install
。
安装
Progress可通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中
pod "Progress"
屏幕截图
基本使用
// start progress
Prog.start(in: imageView, .blur(.dark), .activityIndicator)
// start progress with call back after animation
Prog.start(in: imageView, .blur(.dark), .activityIndicator) {
// do something....
}
// update completion ratio
Prog.update(0.87, in: imageView)
// end progress
Prog.end(in: imageView)
// end progress with call back after animation
Prog.end(in: imageView) {
// do something....
}
// dismiss progress
Prog.dismiss(in: imageView)
// dismiss progress with call back after animation
Prog.dismiss(in: imageView) {
// do something....
}
结束与销毁
调用 Prog.dismiss(in:)
只是简单地移除进度条而不结束动画。例如,对于一个具有 .label
进度条的进度条,Prog.dismiss(in:)
只会淡出百分比标签,而 Prog.end(in:)
则会使百分比达到 100%
然后淡出标签。
内置进度条类型
.sync([ProgressorType])
.color(ColorProgressorParameter?)
.blur(BlurProgressorParameter?)
.activityIndicator
.bar(BarProgressorParameter?)
.ring(RingProgressorParameter?)
.label(LabelProgressorParameter?)
.dismissable
参数
public typealias ColorProgressorParameter = UIColor
public let DefaultColorProgressorParameter: ColorProgressorParameter = UIColor.white.withAlphaComponent(0.5)
public typealias BlurProgressorParameter = UIBlurEffectStyle
public let DefaultBlurProgressorParameter: BlurProgressorParameter = .light
public typealias BarProgressorParameter = (type: BarProgressorType, side: BarProgressorSide, barColor: UIColor, barHeight: CGFloat)
public let DefaultBarProgressorParameter: BarProgressorParameter = (.proportional, .top, UIColor.black.withAlphaComponent(0.5), 2)
public typealias RingProgressorParameter = (type: RingProgressType, color: UIColor, radius: CGFloat, lineWidth: CGFloat)
public let DefaultRingProgressorParameter: RingProgressorParameter = (.proportional, UIColor.black.withAlphaComponent(0.5), 12, 4)
public typealias LabelProgressorParameter = (font: UIFont, color: UIColor)
public let DefaultLabelProgressorParameter: LabelProgressorParameter = (UIFont.systemFont(ofSize: 14), .black)
进度可以从一个父元素中开始带有多个进度条。
- 由于
Prog
对所有ProgressParent
和ProgressView
保持强引用,请务必在进度结束前调用Prog.end(in:)
或Prog.dismiss(in:)
。- 确保在所有动画完成后再更新/结束/销毁进度。
Prog.start(in: view, .blur(nil)) { // do something // ... Prog.end(in: self.view) // Prog.dismiss(in: self.view) }
同步进度条
默认情况下,进度条将被依次添加并开始动画。在结束进度时,进度条将按反向顺序结束动画和移除。开始和结束动画是依次执行的,即异步的。
要有一个同步的进度动画,即同时执行进度条的动画,只需将进度条类型包裹在 .sync
进度条类型中即可。
let ringParam: RingProgressorParameter = (.proportional, UIColor.black.withAlphaComponent(0.4), 40, 1.5)
let labelParam: LabelProgressorParameter = (UIFont.systemFont(ofSize: 20, weight: UIFontWeightLight), UIColor.black.withAlphaComponent(0.6))
Prog.start(in: progressParent, .blur(nil), .sync([.ring(ringParam), .label(labelParam)]))
可 dismiss 的进度条
添加 .dismissable
进度条允许用户通过单击来 dismiss 进度。
Prog.start(in: progressParent, .blur(nil), .sync([.ring(ringParam), .label(labelParam)]), .dismissable)
高级用法
ProgressParent
实现 ProgressParent
协议的类可以添加/移除 ProgressView
。默认情况下,UIView
和 UIViewController
作为 ProgressParent
实现。
UIView
的默认实现是在 0.2 秒的渐变动画中添加 progressView
作为子视图。而 UIViewController
简单调用 self.view
实现。
可以通过设置 Prog.fadingDuration
来配置渐变持续时间。
自定义进度条
自定义进度条视图子类化
import Progress
class CustomProgressorView: ProgressorView {
var label: UILabel = UILabel()
override func layoutSubviews() {
super.layoutSubviews()
label.sizeToFit()
label.center = center
}
// prepareForProgress is executed before being added to ProgressParent
override func prepareForProgress(parameter: Any?) {
addSubview(label)
label.text = "loading..."
}
override func startProgress(parameter: Any?, completion: @escaping (() -> Void)) {
// some starting animation ...
// always call completion at the end of starting progress
completion()
}
override func update(progress: Float) {
// update progress view
let percent = Int(floor(progress*100))
label.text = "loading \(percent)% ..."
setNeedsLayout()
}
override func endProgress(completion: @escaping (() -> Void)) {
UIView.animate(withDuration: 1, animations: {
self.label.text = "DONE!"
self.label.transform = self.label.transform.scaledBy(x: 3, y: 3)
}) { _ in
// always call completion at the end of ending progress
completion()
}
}
}
结束动画持续时间
在通过动画实现
endProgress(completion:)
代替简单调用completion()
时,建议将动画持续时间与剩余进度成比例,最大值为Prog.maxEndingAnimationDuration
。例如,如果进度从 0.6(60%)结束,动画持续时间应该是(1-0.6)*Prog.maxEndingAnimationDuration
。可以通过设置
Prog.maxEndingAnimationDuration
来配置内置进度条的结束动画持续时间。
注册自定义进度器视图
Prog.register(progressor: CustomProgressorView.self, withIdentifier: "custom_example")
当作内置使用
Prog.start(in: imageView, .customer(identifier: "custom_example", parameter: nil))
这里的数据类型 parameter: Any?
将被传递给
progressorView.prepareForProgress(parameter: Any?)
progressorView.startProgress(parameter: Any?, completion: @escaping (() -> Void))
作者
许可
进度功能遵循MIT许可。有关更多信息,请参阅LICENSE文件。