I want to animate the appearance of an item in a list. The list looks like this:
Text("Jim") Text("Jonas") TextField("New Player") TextField("New Player") //should be animated when appearing (not shown until a name is typed in the first "New Player") The last TextField should be hidden until newPersonName.count > 0 and then appear with an animation.
This is the code:
struct V_NewSession: View { @State var newPersonName: String = "" @State var participants: [String] = [ "Jim", "Jonas" ] var body: some View { VStack(alignment: .leading) { ForEach(0...self.participants.count + 1, id: \.self) { i in // Without this if statement, the animation works // but the Textfield shouldn't be shown until a name is typed if (!(self.newPersonName.count == 0 && i == self.participants.count + 1)) { HStack { if(i < self.participants.count) { Text(self.participants[i]) } else { TextField("New Player", text: $newPersonName, onEditingChanged: { focused in if (self.newPersonName.count == 0) { if (!focused) { handleNewPlayerEntry() } } }) } } } } .transition(.scale) Spacer() } } func handleNewPlayerEntry() { if(newPersonName.count > 0) { withAnimation(.spring()) { participants.append(newPersonName) newPersonName = "" } } } } I know withAnimation(...) only applies to participants.append(newPersonName), but how can I get the animation to work on the property change in the if-statement? if ((!(self.newPersonName.count == 0 && i == self.participants.count + 1)).animation()) doesn't work.