swift - 阻止 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()
}
}
解决方案
给你一个完美的答案
.animation( Animation.easeInOut.repeatForever(), value: self.heart)
这将保证动画在self.heart
更改后重新启动
推荐阅读
- node.js - 如何在 MongoDB 中保存小数高度
- ios - 根据字段值 Swift 分组对象
- tensorflow - 在 Tensorflow 中使用动态时间环绕 (DTW) 功能测量两条曲线之间的距离
- r - mlogit 包中的错误 R 缺少需要 TRUE/FALSE 的值
- javascript - 从可迭代的 JS 对象中获取上一个项目?
- python - 来自网络文本文档的字数统计结果为 0
- javascript - 有没有办法使用 express 和 hbs 路由到特定的 div?
- azure - Azure 应用服务证书 - 无法自动续订
- python - 您可以将列表中的多个项目合并并在最后添加“x(数量)”吗?
- javascript - eventListeners 的动画处理问题