SwiftUI ForEach 没有动画延迟

Posted

技术标签:

【中文标题】SwiftUI ForEach 没有动画延迟【英文标题】:SwiftUI ForEach no animation delay 【发布时间】:2019-12-14 01:21:27 【问题描述】:

我最近更新到 Xcode 11.3,现在我的 ForEach 语句中的一个以前工作的动画延迟失败了。下面是我的代码的简化版本。

谢谢

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)
    

【问题讨论】:

我看到越来越多的区域在更新到 Xcode 11.3 后被报告为损坏... 【参考方案1】:

这可能是由于双 VStack。您可以在 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)

【讨论】:

以上是关于SwiftUI ForEach 没有动画延迟的主要内容,如果未能解决你的问题,请参考以下文章

为 ForEach 循环元素添加动画 (SwiftUI)

SwiftUI 反向动画延迟移除

SwiftUI:如何使用延迟在两个图像之间设置动画过渡?

延迟 SwiftUI 中的重复动画,在完整的自动反转重复周期之间

SwiftUI:删除列表中的动画?

Swiftui NavigationView 计时器延迟