CircularProgress

macOS 应用程序的圆形进度指示器

此软件包已在生产环境中使用,例如 GifskiHEIC Converter 等应用程序。

要求

macOS 10.15+

安装

在 Xcode 的 “Swift Package Manager” 标签页中添加 https://github.com/sindresorhus/CircularProgress

最新版本:3.0.1

用法

另请查看 Xcode 项目中的示例应用程序。

手动设置进度

import Cocoa
import CircularProgress

@main
final class AppDelegate: NSObject, NSApplicationDelegate {
	@IBOutlet private var window: NSWindow!

	let circularProgress = CircularProgress(size: 200)

	func applicationDidFinishLaunching(_ notification: Notification) {
		window.contentView!.addSubview(circularProgress)

		foo.onUpdate = { progress in
			self.circularProgress.progress = progress
		}
	}
}

指定一个 Progress 实例

只要 CircularProgress 实例存在,或者直到您设置 .progressInstance = nil,给定的 Progress 实例都会被强引用保持活动状态。

import Cocoa
import CircularProgress

@main
final class AppDelegate: NSObject, NSApplicationDelegate {
	@IBOutlet private var window: NSWindow!

	let circularProgress = CircularProgress(size: 200)
	let progress = Progress(totalUnitCount: 1)

	func applicationDidFinishLaunching(_ notification: Notification) {
		window.contentView!.addSubview(circularProgress)

		progress?.becomeCurrent(withPendingUnitCount: 1)
		circularProgress.progressInstance = progress
	}
}

取消按钮

如果您使用 .progress 属性,则需要通过设置 .isCancellable = true 来选择启用取消按钮。您可以通过将 .onCancelled 属性设置为闭包来接收按钮点击的通知。

如果您使用 .progressInstance 属性,设置一个 isCancellableProgress 对象(这是默认设置),将自动启用取消按钮。

默认情况下,取消状态通过降低当前颜色的饱和度和不透明度来指示。您可以通过实现 .cancelledStateColorHandler 回调并返回要用于取消状态的颜色来自定义此行为。当回调被设置后,不透明度不会自动降低。要完全禁用取消状态的可视化效果,请将 .visualizeCancelledState 设置为 false

不确定状态

显示一个状态,指示剩余进度是不确定的。

请注意,.progress 属性和 .isIndeterminate 没有关联。当进度再次进行时,您需要手动设置 .isIndeterminate = false

如果您使用 .progressInstance 属性,isIndeterminate 属性将被自动观察。视图将在适当的时候来回切换到不确定状态。

隐藏进度标签

显示一个微调器,中心没有百分比指示器。

这可以通过将 .isLabelHidden 属性设置为 true 来实现。默认状态为 false(标签显示)。

API

/**
Color of the circular progress view.
*/
@IBInspectable var color: NSColor = .controlAccentColor

/**
Line width of the circular progress view.
*/
@IBInspectable var lineWidth: Double = 2

/**
Show an animated checkmark instead of `100%`.
*/
@IBInspectable var showCheckmarkAtHundredPercent = true

/**
Hide the progress label.

The property supports KVO.
*/
@IBInspectable var isLabelHidden = true

/**
The progress value in the range `0...1`.

- Note: The value will be clamped to `0...1`.
*/
@IBInspectable var progress: Double = 0

/**
Let a `Progress` instance update the `progress` for you.
*/
var progressInstance: Progress?

/**
Reset the progress back to zero without animating.
*/
func resetProgress() {}

/**
Cancels `Progress` if it's set and prevents further updates.
*/
func cancelProgress() {}

/**
Triggers when the progress was cancelled succesfully.
*/
var onCancelled: (() -> Void)?

/**
Returns whether the progress is finished.

The property supports KVO.
*/
@IBInspectable var isFinished: Bool { get }

/**
If the progress view is cancellable it shows the cancel button.
*/
@IBInspectable var isCancellable: Bool

/**
Make the progress indeterminate.

The property supports KVO.
*/
@IBInspectable var isIndeterminate: Bool

/**
Returns whether the progress has been cancelled.

The property supports KVO.
*/
@IBInspectable var isCancelled: Bool { get }

/**
Determines whether to visualize changing into the cancelled state.
*/
var visualizeCancelledState: Bool = true

/**
Supply the base color to use for displaying the cancelled state.
*/
var cancelledStateColorHandler: ((NSColor) -> NSColor)?

init(frame: CGRect) {}
init?(coder: NSCoder) {}

/**
Initialize the progress view with a width/height of the given `size`.
*/
convenience init(size: Double) {}

相关