Animation 隐式路径动画的SwiftUI问题

Animation 隐式路径动画的SwiftUI问题,animation,path,swiftui,Animation,Path,Swiftui,我有下面的代码,它通过状态变量通过隐式动画绘制曲线。每次滑块更改值时,曲线的参数都会更改,因此会重新绘制另一条曲线。尽管代码在第一次运行时按预期工作,并且曲线已设置动画,但后续曲线的情况并非如此。绘制了一条新曲线,但未设置动画。 代码在这里 import SwiftUI import CoreData struct LissajousView: View { @State private var aSliderValue: Double = 4 @State priv

我有下面的代码,它通过状态变量通过隐式动画绘制曲线。每次滑块更改值时,曲线的参数都会更改,因此会重新绘制另一条曲线。尽管代码在第一次运行时按预期工作,并且曲线已设置动画,但后续曲线的情况并非如此。绘制了一条新曲线,但未设置动画。 代码在这里

import SwiftUI
import CoreData

struct LissajousView: View {
    
    @State private var aSliderValue: Double = 4
    @State private var bSliderValue: Double = 3
    @State private var deltaSliderValue: Double = 2
    @State private var percentage = CGFloat(0.0)
    
            
    var body: some View {
        
        let results = calcLissajous(a: aSliderValue, b: bSliderValue, d: deltaSliderValue)
        let lineAnimation = Animation.easeInOut(duration: 2)
        
        ZStack(alignment: .center) {

            VStack(alignment: .trailing)    {
                StrokeText(text: "Lissajous", color: UIColor.red)
                    .fixedSize()
                Text("Shapes")
                    .font(.custom("Cochin-Italic", size: 26))
                    .foregroundColor(.gray)
                
                Spacer()
                
                RoundedRectangle(cornerRadius: 10, style: .continuous)
                    .fill(Color.black)
                    .frame(width: 350, height: 350)
                    .overlay(
                        Path { path in
                            path.move(to: results[0])
                            for i in 1..<results.count  {
                                path.addLine(to: results[i])
                            }
                        }
                        .trim(from: 0, to: percentage)
                        .stroke(Color.green, lineWidth: 2)
                        .frame(width: 340, height: 340)
                        .animation(lineAnimation)
                        .onAppear {percentage = 1.0}
                    )
                
                Spacer(minLength: 300)
                
            }

            VStack(alignment: .center, spacing: 25)    {
                
                Spacer(minLength: 470)
                
                HStack(alignment: .center, spacing: 50) {
                    Text("x = sin(at + φ)")
                    Text("y = sin(bt)")
                }
                .font(.custom("AmericanTypewriter", size: 20))
                                      
                
                HStack(alignment: .center, spacing: 5) {
                    Spacer()
                    Text("a=\(aSliderValue, specifier: "%.1f")")
                        .font(.custom("AmericanTypewriter", size: 20))
                    Spacer(minLength: 10)
                    Slider(value: $aSliderValue, in: 1...10, step: 0.5)
                        .accentColor(Color.green)

                    Spacer()
                }
                
                HStack(alignment: .center, spacing: 5) {
                    Spacer()
                    Text("b=\(bSliderValue, specifier: "%.1f")")
                        .font(.custom("AmericanTypewriter", size: 20))
                    Spacer(minLength: 10)
                    
                    Slider(value: $bSliderValue, in: 1...10, step: 0.5)
                        .accentColor(Color.green)
                    
                    Spacer()
                }
                
                HStack(alignment: .center, spacing: 5) {
                    Spacer()
                    Text("φ=π/\(deltaSliderValue, specifier: "%.0f")")
                        .font(.custom("AmericanTypewriter", size: 20))
                    Spacer()
                    Slider(value: $deltaSliderValue, in: 1...16, step: 1)
                        .accentColor(Color.green)
                    Spacer()
                }
                
                Spacer(minLength: 30)
            }
        }
    }
}
导入快捷界面
导入CoreData
结构LissajousView:视图{
@国家私有变量aSliderValue:Double=4
@国家私有变量bSliderValue:Double=3
@国家私有变量deltaSliderValue:Double=2
@国有-私有风险值百分比=CGFloat(0.0)
var body:一些观点{
让结果=calcLissajous(a:aSliderValue,b:bSliderValue,d:deltaSliderValue)
让lineAnimation=Animation.easeInOut(持续时间:2)
ZStack(对齐:。中心){
VStack(对齐:。尾部){
StrokeText(文本:“李萨如”,颜色:UIColor.red)
.fixedSize()
文本(“形状”)
.font(.custom(“Cochin Italic”,大小:26))
.foregroundColor(.灰色)
垫片()
圆角转角(拐角半径:10,样式:连续)
.填充(颜色.黑色)
.框架(宽:350,高:350)
.覆盖(
路径{Path in
移动路径(到:结果[0])
因为我在1。。