简体   繁体   中英

SwiftUI ForEach no animation delay

I recently updated to Xcode 11.3 and now a perviously working animation delay in my ForEach statement has failed. Below is a simplified version of my code.

Thanks

import SwiftUI

struct ContentView: View {

    @State var show: Bool = false

        var transition: AnyTransition {

            let insertion = AnyTransition.move(edge: .trailing)

            let removal = AnyTransition.move(edge: .leading)

            return .asymmetric(insertion: insertion, removal: removal)
        }

    var body: some View {
        VStack {

            Button(action: { withAnimation { self.show.toggle() } } ) {
                Text("start animation")
            }
            if show == true {
                test()
                .transition(transition)
            }
        }
    }

}


struct wordArray: Identifiable{

    var id = UUID()
    var words: String

}

struct test: View{

    let circleArray = [

        wordArray(words: "This"),
        wordArray(words: "Should"),
        wordArray(words: "Be"),
        wordArray(words: "Delayed"),

    ]

    var body: some View{

        VStack{

            ForEach(circleArray) { wordArray in
                Text("\(wordArray.words)")
                    .animation(Animation.easeInOut.delay(0.5))
            }

            Text("like")
                .animation(Animation.easeInOut.delay(0.5))

            Text("This")
                .animation(Animation.easeInOut.delay(1))


        }.frame(width: UIScreen.main.bounds.width)
    }
}

It may be due to the double VStack. You can change the VStack to Group in testView.

     Group{

        ForEach(circleArray) { wordArray in
            Text("\(wordArray.words)")
                .animation(Animation.easeInOut.delay(0.5))
        }

        Text("like")
            .animation(Animation.easeInOut.delay(0.5))

        Text("This")
            .animation(Animation.easeInOut.delay(1))


    }.frame(width: UIScreen.main.bounds.width)

I found a solution to the problems related to, in many cases, views inside ForEach not working with either transitions or animations.

The first solution was to contain the items in a ForEach within a List view.

The other option is to store the group of views in the ForEach , within a ScrollView , which is my preferred options, as a list view comes with a great deal of limitations in what and how you can render it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM