簡體   English   中英

SwiftUI ForEach 無 animation 延遲

[英]SwiftUI ForEach no animation delay

我最近更新到 Xcode 11.3,現在我的ForEach語句中以前有效的 animation 延遲失敗了。 下面是我的代碼的簡化版本。

謝謝

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)
    }
}

這可能是由於雙 VStack。 您可以在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)

在許多情況下,我找到了與ForEach內的視圖不支持過渡或動畫相關的問題的解決方案。

第一個解決方案是將項目包含在List視圖中的ForEach中。

另一種選擇是將視圖組存儲在ForEach中的ScrollView中,這是我的首選選項,因為列表視圖在渲染內容和方式方面有很多限制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM