首页 > 解决方案 > 阻止 SwiftUI 动画进行水平移动

问题描述

我有一个正在创建心跳模式的动画。问题是,一旦文本(同一个HStack)改变了合理的边距,动画就会水平振荡,因为它是被文本推动的。我怎样才能阻止这种情况发生?有没有办法锚定图像,以免发生这种戏剧性的水平运动?

        HStack(alignment: .bottom){
            Text("\(self.hr)") +  Text("BPM")
            Image(systemName: "heart.fill")
                .padding(.init(top: 5, leading: 5, bottom: 10, trailing: 5))
                .foregroundColor(self.outdatedHR ? Color.gray : Color.red)
                .scaleEffect(self.heart ? 1.05: 0.95, anchor: .center)
                .opacity(self.heart ? 1.0: 0.75)
                .animation(Animation.easeInOut.repeatForever())
                .onAppear{
                    self.heart.toggle()
            }

        }

在此处输入图像描述

标签: swiftanimationswiftui

解决方案


给你一个完美的答案

 .animation( Animation.easeInOut.repeatForever(), value: self.heart)

这将保证动画在self.heart更改后重新启动


推荐阅读