在 SwiftUI 中切换视图的最佳方法是什么?

5 performance swift swiftui

我尝试了几个选项来在 SwiftUI 中切换视图。然而,每一个都存在多次切换时会出现时间滞后等问题。链接的问题已被删除,我不知道为什么会这样,但如果不是,它真的可以帮助我。我问这个是因为那个问题引起了我的兴趣,我想知道更多,因为我能够复制该问题中描述的行为。我试图找到使用 SwiftUI 切换视图的最佳和最干净的方法。我只是想制作一个多视图用户界面。

在 View1.swift 中

import SwiftUI
struct View1: View {
    @State var GoToView2:Bool = false
    var body: some View {
        ZStack {
            if (GoToView2) {
                View2()
                //What should I do if I created another swiftui view under the name View2? 
                //Just calling View2() like that causes lag as described in the linked question before it was deleted, if from view2 I switch back to view1 and so on. 
                //If I directly put the code of View2 here, then adding other views would get too messy.
            } else {
                VStack {
                    Button(action: {self.GoToView2.toggle()}) {
                        Text("Go to view 2")
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在 View2.swift 中:

import SwiftUI
struct View2: View {
    @State var GoToView1:Bool = false
    var body: some View {
        ZStack {
            if (GoToView1) {
                 View1()
            } else {
                VStack {
                    Button(action: {self.GoToView1.toggle()}) {
                        Text("Go to view 1")
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望你们能理解这个问题。要复制该行为,请在 swiftUI 应用程序中编译代码,然后在两个按钮之间快速重复切换 30 秒,然后您应该注意到每次切换之间的延迟,并且调整窗口大小应该看起来很粗。我使用的是最新版本的 macOS 和最新版本的 Xcode。

krj*_*rjw 5

所以我试图表明对 的每个调用都会Views向视图堆栈添加一个实例...我在这里可能是错的,但以下应该显示这一点:

struct View1: View {
    @State var GoToView2:Bool = false
    var counter: Int

    init(counter: Int) {
        self.counter = counter + 1
    }

    var body: some View {
        VStack {
            if (GoToView2) {
                Text("\(self.counter)")
                View2(counter: self.counter)
            } else {
                VStack {
                    Button(action: {
                        withAnimation {
                            self.GoToView2.toggle()
                        }
                    }) {
                        Text("Go to view 2")
                    }
                }
            }
        }
    }
}

struct View2: View {
    @State var GoToView1:Bool = false
    var counter: Int

    init(counter: Int) {
        self.counter = counter + 1
    }

    var body: some View {
        VStack {
            if (GoToView1) {
                Text("\(self.counter)")
                View1(counter: self.counter)
            } else {
                VStack {
                    Button(action: {
                        withAnimation {
                            self.GoToView1.toggle()
                        }
                    }) {
                        Text("Go to view 1")
                    }
                }.transition(.move(edge: .leading))
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图证明其他方法不会这样做:

struct View1: View {
    @State var GoToView2: Bool = false
    var counter: Int

    init(counter: Int) {
        self.counter = counter + 1
    }

    var body: some View {
        VStack {
            if (GoToView2) {
                Text("\(self.counter)")
                View2(counter: self.counter, GoToView1: self.$GoToView2)
            } else {
                VStack {
                    Button(action: {
                        withAnimation {
                            self.GoToView2.toggle()
                        }
                    }) {
                        Text("Go to view 2")
                    }
                }
            }
        }
    }
}

struct View2: View {
    @Binding var GoToView1: Bool
    var counter: Int

    init(counter: Int, GoToView1: Binding<Bool>) {
        self._GoToView1 = GoToView1
        self.counter = counter + 1
    }

    var body: some View {

            VStack {
                Text("\(self.counter)")
                Button(action: {
                    withAnimation {
                        self.GoToView1.toggle()
                    }
                }) {
                    Text("Go to view 1")
                }
            }.transition(.move(edge: .leading))


    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道滞后是否真的来自于此,或者是否有更好的证明方法,但现在这就是我想出的。

原答案

我建议执行以下操作:

struct View1: View {
    @State var GoToView2:Bool = false
    var body: some View {
        ZStack {
            if (GoToView2) {
                View2(GoToView1: self.$GoToView2)
            } else {
                VStack {
                    Button(action: {
                        withAnimation {
                            self.GoToView2.toggle()
                        }
                    }) {
                        Text("Go to view 2")
                    }
                }
            }
        }
    }
}

struct View2: View {
    @Binding var GoToView1: Bool
    var body: some View {
        VStack {
            Button(action: {
                withAnimation {
                    self.GoToView1.toggle()
                }
            }) {
                Text("Go to view 1")
            }
        }.transition(.move(edge: .leading))
    }
}
Run Code Online (Sandbox Code Playgroud)