1

I've been trying to get changes to my list elements to animate correctly. However, items in a list don't seem to animate as specified.

In this simple example, an element is removed. There is an animation, within 1 second the element is removed. However, it completely ignores the duration and delay.

struct ContentView: View {
    
    @State var items = [1, 2, 3, 4, 5]
    
    var body: some View {
        VStack {
            List {
                ForEach(items, id: \.self) { item in
                    Text("Item \(item)")
                }
            }
            
            Button {
                withAnimation(Animation.easeInOut(duration: 5).delay(1)) {
                    
                    print("removing element")
                    items.removeFirst()
                }
            } label: {
                Text("Remove element")
            }

        }
    }
}

If I remove the List and just have a VStack of items, the Animation parameter is processed correctly.

If I remove the withAnimation, it doesn't animate at all. So it is triggering it.

1 Answers1

0

I would say that this is expected as this is how underlying UITableViewController is working, it doesn't have capabilities to customise animations that way.

I think that if you want to do something custom you need to start with LazyVStack in ScrollView, that will give you more space for creation and it is quite likely that this would work (I haven't tried it, but from the logical point it should).

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57