如何解决AnimatableModifier仅适用于递增值,不适用于递减SwiftUI
我正在使用AnimatableModifier对[[46],[11],[3],[23],[22],[33],[12]]
值进行动画处理。递增值时动画工作正常,而递减时动画不工作。
所以我有一个列表,其中有项目。所有这些项的总数在视图顶部列出。当插入一个新项目时,总数将增加,而我得到该动画。但是,在删除项目时我没有获得动画。只会降低它的值。
"basicCard": {
"title": "","subtitle": "","formattedText": "","image": {},"buttons": [
{
"title": "1-855-xxx-xxxx","openUrlAction": {
"url": "tel:+1855xxxxxxx","urlTypeHint": 0
}
},{}
]
}
}
这是我使用此修饰符的方式:
CGFloat
struct TotalExpenseAnimator: AnimatableModifier {
var pct: CGFloat = 0.0
var animatableData: CGFloat {
get { pct }
set {pct = newValue }
}
func body(content: Content) -> some View {
VStack(spacing: 0) {
content
TotalExpenseView2(pct: pct)
}
}
}
struct TotalExpenseView2: View {
let pct: CGFloat
var body: some View {
HStack(spacing: 0) {
Text(formatPrices(price: self.pct))
//.bold()
.font(Font.system(.title2))
// .padding(.top,8)
.foregroundColor(Color("TextColorWidget"))
}.onChange(of: self.pct) { (newVal) in
print(newVal) //-> val changes here so this whole thing is getting called
}
}
func formatPrices(price: CGFloat) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
numberFormatter.locale = Locale.current
numberFormatter.maximumFractionDigits = 2
return numberFormatter.string(from: price as NSNumber) ?? ""
}
是一个 Text("Total Expense")
.font(Font.footnote)
.foregroundColor(Color.secondary)
.modifier(TotalExpenseAnimator(pct: ListTotalExpense(financedList: self.financedList).totalFloat))
,我将其传递给该列表,并计算所有费用并在ListTotalExpense
中获得总额
之所以调用observableObject,是因为我可以获得新值(递减值),但是根本没有动画。是因为在删除行时刷新了整个视图,导致动画被强制绕过了吗?
任何想法/建议将不胜感激!谢谢:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。