[英]How do I animate changes one at a time in SwiftUI on a button tap?
我有一个循环,我在其中更新一个更改视图的值。 在这个例子中,我希望更新一次发生一个,所以你会看到它在行中波动,但它们都是同时发生的。
struct AnimationQuestion: View {
@State var colors = "🟪🟨🟧🟦🟥🟫⬛️🟩⬜️".map { String($0) }
@State var reversedColors = "⬜️🟩⬛️🟫🟥🟦🟧🟨🟪".map { String($0) }
var body: some View {
VStack {
Spacer()
Button("Tap") {
let temp = colors
for i in 0 ..< colors.count {
withAnimation(.linear(duration: 4.0)) {
colors[i] = reversedColors[i]
}
}
reversedColors = temp
}
Spacer()
HStack {
ForEach(Array(colors.enumerated()), id: \.0) { index, color in
Text(color).tag(index)
}
}
Spacer()
}
}
}
struct QuestionPreview: PreviewProvider {
static var previews: some View {
AnimationQuestion()
}
}
我希望这些行按顺序(明显)执行 animation:
withAnimation(.linear(duration: 4.0)) {
colors[i] = reversedColors[i]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.