Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I animate changes one at a time in SwiftUI on a button tap?

I have a loop where I update a value that changes the view.
In this example I want the updates to happen one at a time so you see it ripple across the row, but they all happen at once.

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()
   }
}

I expected these lines to perform the animation sequentially (visibly):

    withAnimation(.linear(duration: 4.0)) {
         colors[i] = reversedColors[i]
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You identify by index \.0, but indices do not change after reversing, so refresh is instant, instead you need to identify by color value \.1 (in this case they are unique).

Here is a fix

enter image description here

ForEach(Array(colors.enumerated()), id: \.1) { index, color in
   Text(color).tag(index)
}

Tested with Xcode 13.3 / iOS 15.4

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading