1

I'm creating a view and would like to show cells being filled after view appear.

enter image description here

here is the code:

import SwiftUI

struct AnimatedListView: View {
    @State private var items: [String] = []
    
    var body: some View {
        List {
            ForEach($items, id: \.self) { $item in
                Text(item)
            }
            .animation(.easeInOut, value:items) // Apply animation to each cell
            
            if items.isEmpty {
                Text("No items")
                    .foregroundColor(.gray)
            }
        }
        .onAppear {
            withAnimation{
                animateList()
            }
        }
    }
    
    private func animateList() {
        let initialItems = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]
        
        for (index, item) in initialItems.enumerated() {
            DispatchQueue.main.asyncAfter(deadline: .now() + Double(index) * 0.2) {
                items.append(item)
            }
        }
    }
}

// Usage example
struct ContentView: View {
    var body: some View {
        AnimatedListView()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I would like to get the .default animation of add cell in list.

Any thoughts?

Sebastian
  • 6,154
  • 5
  • 33
  • 51

0 Answers0