UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

1      

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

2      

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

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.