CircularProgressMac 2.2.0

CircularProgressMac 2.2.0

Sindre Sorhus 维护。



CircularProgress

为您的 macOS 应用提供圆形进度指示器

此包被 Gifski 和 HEIC Converter 等应用在生产中使用。

需求

  • macOS 10.12+
  • Xcode 11.4+
  • Swift 5.2+

安装

Swift 包管理器

在 Xcode 的 “Swift Package Manager” 选项卡中添加 https://github.com/sindresorhus/CircularProgress

Carthage

github "sindresorhus/CircularProgress"

CocoaPods

pod 'CircularProgressMac'

使用

也可以查看 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 属性,设置一个 Progress 对象,使其 isCancellable,默认情况,自动启用取消按钮。

默认情况下,取消状态通过禁用当前颜色并减少不透明度来指示。您可以通过实现 .cancelledStateColorHandler 回调并返回用于取消状态的颜色来自定义此操作。在设置回调时,不透明度不会自动减少。要完全禁用取消状态的可视化,请将 .visualizeCancelledState 设置为 false

不确定状态

显示出一个不确定剩余进度的状态。

注意,.progress 属性和 .isIndeterminate 并不捆绑在一起。当进度再次进行时,您需要手动将 .isIndeterminate = false 设置。

如果使用 .progressInstance 属性,isIndeterminate 属性将自动被观察。当合适时,视图将根据需要在此不确定状态之间切换。

API

/**
Color of the circular progress view.

Defaults to the user's accent color. For High Sierra and below it uses a fallback color.
*/
@IBInspectable var color: NSColor = .controlAccentColor

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

/**
Show an animated checkmark instead of `100%`.
*/
@IBInspectable var showCheckmarkAtHundredPercent: Bool = 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

/**
Displays the indeterminate state.
*/
@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) {}

相关

维护者