动画圆形ProgressView()

问题描述 投票:0回答:1

在我的 macOS Swift 应用程序中,我尝试添加一个

ProgressView()
,首先是线性的,然后是圆形的。 我可以轻松地为线性动画制作动画,但不能为圆形动画制作动画。

这是我的代码:

var progressAction1 = 0.0

// linear
ProgressView(value: progressAction1ProgressView(value: progressAction1)
    .animation(.easeIn(duration: 1), value: progressAction1)
    // .padding(.horizontal, 20)

// circular
ProgressView(value: progressAction1)
    .animation(.easeIn(duration: 1), value: progressAction1)
    .progressViewStyle(.circular)
    // .padding(.horizontal, 20) 
macos swiftui progress-bar
1个回答
0
投票

macOS 上的循环进度视图似乎根本不支持开箱即用的动画。希望未来的 macOS 版本能够实现这一点。

您可以通过围绕

Animatable
创建一个
ProgressView
包装器来完成这项工作。

struct AnimatableCircularProgressView<Value: BinaryFloatingPoint & VectorArithmetic>: View, Animatable {
    var progress: Value
    
    var animatableData: Value {
        get { progress }
        set { progress = newValue }
    }
    
    var body: some View {
        ProgressView(value: progress)
            .progressViewStyle(.circular)
    }
}

用途:

struct ContentView: View {

    @State private var progressAction1 = 0.0
    
    var body: some View {
        AnimatableCircularProgressView(progress: progressAction1)
            .animation(.easeIn(duration: 1), value: progressAction1)
        Button("Foo") {
            progressAction1 += 0.5
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.