简体   繁体   English

SwiftUI ForEach 无 animation 延迟

[英]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.我最近更新到 Xcode 11.3,现在我的ForEach语句中以前有效的 animation 延迟失败了。 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.这可能是由于双 VStack。 You can change the VStack to Group in testView.您可以在VStack更改为Group

     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.在许多情况下,我找到了与ForEach内的视图不支持过渡或动画相关的问题的解决方案。

The first solution was to contain the items in a ForEach within a List view.第一个解决方案是将项目包含在List视图中的ForEach中。

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.另一种选择是将视图组存储在ForEach中的ScrollView中,这是我的首选选项,因为列表视图在渲染内容和方式方面有很多限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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