如何防止圆形动画反转?

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

我的圈子是倒计时。变量

pct
是一个变化的变量,范围从 0.0 到 0.99 左右。问题是,一旦变量为 1.0,它就会向后动画化。我怎样才能让它再次从 0 开始?

enter image description here

struct ActivityCircle : View {
    let color: UIColor
    let pct: CGFloat
    
    @State var progress: CGFloat = 0.0
    
    
    var body: some View {
        Circle()
            .trim(from: 0, to: progress)
            .rotation(Angle(degrees: -90))
            .stroke(style: StrokeStyle(lineWidth: 13, lineCap: .round, lineJoin: .round))
            .foregroundColor(Color(color))
        // .animation(Animation.linear(duration: 1), value: progress)
            .onAppear{
                print("circle appears")
                //                                withAnimation(.linear(duration: 20)){
                //                                    progress = 1
                //                                }
                
            }
            .onChange(of: pct)  { oldValue, newValue in
                print("Old value is \(oldValue)")
                print("New value is \(newValue)")
                withAnimation(.linear(duration: 1.0)) {
                    
                    if(newValue == 0){
                        progress = 1.0
                    } else {
                        progress = newValue
                    }
                }
                
            }
        
    }
}
swift swiftui
1个回答
0
投票

看看这个答案:https://www.hackingwithswift.com/forums/swiftui/instantly-reset-state-of-animation/4494

更改组件的 id 将重置视图

© www.soinside.com 2019 - 2024. All rights reserved.