圆形旋转器 (CircularSpinner)

一个 Swift 包,用于在 SwiftUI 中绘制动画圆形旋转器。希望您喜欢!

圆形旋转器是一个带有旋转前景圆点的描边背景圆圈。圆点的长度在每个旋转周期中在长和短之间振荡。您只需在您的视图层级结构中声明一个 CircularSpinner,它就会自动动画。

描边的前景和背景可以是任何 ShapeStyle。 这允许将阴影和渐变等特殊效果应用于任一层。 默认情况下,旋转器将填充其容器的边界,但您可以向初始化器提供直径。

Example GIF

API (应用程序编程接口)

有两种初始化器。 主要初始化器提供最多的自定义,接受单独的背景和前景 ShapeStyle

/// Initializes an indefinitely animating CircularSpinner.
/// - Parameters:
///   - strokeWidth: The stroke width to use for the spinner background and foreground circles.
///   - animationDuration: The duration of one spin cycle, which rotates the puck 2 full revolutions around the circle.
///   - diameter: The diameter of the spinner. When nil, the spinner fills the container's bounds.
///   - backgroundStyle: The ShapeStyle to apply to the full background circle.
///   - foregroundStyle: The ShapeStyle to apply to the spinning puck.
public init(strokeWidth: CGFloat = 10,
            animationDuration: TimeInterval = 1.75,
            diameter: CGFloat? = nil,
            backgroundStyle: Background = Color.secondary,
            foregroundStyle: Foreground = Color.primary)

使用案例示例

CircularSpinner(
    strokeWidth: 15,
    animationDuration: 1,
    backgroundStyle: .white.shadow(.inner(radius: 3)),
    foregroundStyle: .red.shadow(.inner(radius: 3))
)

预制样式

还有第二个初始化器,它接受预制的 SpinnerStyle

public init(strokeWidth: CGFloat = 10,
            animationDuration: TimeInterval = 1.75,
            diameter: CGFloat? = nil,
            style: SpinnerStyle)

使用案例示例

CircularSpinner(style: .bubbledForeground)

以下是每个 SpinnerStyle 的示例

bubbledForeground (气泡前景) hierarchy (层级) accent (强调色) nineties (九十年代风格)
bubbledForeground hierarchy accent nineties