WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Day 31: Animating inside for _ in

Forums > 100 Days of SwiftUI

Hi, everyone! I'm working on Challenge #2 of Project 5 where we're asked to restart with a new word.

The easiest way would be just redeclaring usedWords array as an empty array, but I wanted to explore further and add an animation where all elements in usedWords dissapeear with an animation, like we did with inserting a new word,

withAnimation {
            usedWords.insert(answer, at: 0)
        }

So I wrote a for _ in loop that removes every element in usedWords one by one from the start of the arrary as below.

func resetGame() {
        userScore = 0
        for _ in usedWords {
            withAnimation {
            usedWords.remove(at: 0)
            }
        }
    }

However, this gives me a warning "Result of call to 'withAnimation' is unused". It does do the job of removing all elements from usedWords so surely the loop works successfully, but I don't see an animation as the warning suggested.

Any tips on how to implement this feature?

   

Lulu wonders why her procedural code doesn't work in Swift's declarative environment!

func resetGame() {
        userScore = 0
        for _ in usedWords {
            withAnimation {
            usedWords.remove(at: 0)
            }
        }
    }

Ok, I admit. I made some assumptions that you have a background in procedural programming languages. It seems this way to me because I interpret your code in this way:

Set the userScore to zero
Then for each word in the array, remove the first word.
When removing the word, apply some animation!
Keep going until all the words are removed.

In SwiftUI's declarative world, you should tell Swift to draw a list using words in the usedWords array. But Swift should draw the list of words using animation whenever the newGame value changes!

This ensures the view is redrawn with animation whenever the newGame boolean is reset.

//  RemoveWordAnimation
//  Created by Obelix on 5/3/22.

import SwiftUI

struct ShowWords: View {
    @State private var newGame = false  // For testing only. Force a newGame
    // This array changes, based on your Toggle selection
    private var usedWords: [String] {
        return newGame ? [String]() : ["Lorem", "ipsum", "dolor", "sit",  "amet", "consectetur"]
    }
    var body: some View {
        VStack {
            Toggle("New Game", isOn: $newGame).padding(.horizontal)
            List {
                ForEach(usedWords, id: \.self) { someWord in
                    Text("\(someWord)")
                }
                // DECLARE this animation happens whenever the newGame variable changes.
                // No need to cycle through all the rows, remove each one, and force animation.
            } .animation(.easeOut(duration: 0.75), value: newGame)
        }
    }
}

1      

Thank you so much! I started the following lessons on animations and it made much more sense as well!

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.