Отображать только один вид за раз, чтобы пользователь мог переключаться между представлениями - PullRequest
1 голос
/ 26 февраля 2020

Проблема:

У меня есть 3 отдельных вида, которые я отображаю одновременно, но я хочу, чтобы одновременно отображался только один вид, чтобы пользователь мог просто переключаться между тремя видами. Вот что сейчас происходит.

All the views are displayed

One view is displayed but I can't toggle between the views and have to open all of them together

Код приведен ниже, но при копировании и вставке кода в Xcode вы можете увидеть, что происходит сейчас. У меня есть три вида, которые имеют разные размеры кадров. Вы можете нажать каждую кнопку просмотра, и она откроет это представление.

import SwiftUI

struct ContentView: View {
    @State var show1 = false
    @State var show2 = false
    @State var show3 = false

    var body: some View {
                ZStack {
                          NavigationView {
                            VStack {
                                Section {
                                    Color(red: 0.88, green: 0.88, blue: 0.88)
                                    .frame(maxWidth: .infinity, maxHeight: .infinity).edgesIgnoringSafeArea(.all)
                                 HStack {

                                    Button(action: {
                                        withAnimation(.spring())
                                        {
                                            self.show1.toggle()
                                        }
                                    }) {
                                        Text("View1")
                                            .font(.headline)
                                            .fontWeight(.regular)
                                            .lineLimit(nil)
                                            .padding(EdgeInsets.init(top: -5, leading: 15, bottom: 0, trailing: 15))
                                        .foregroundColor(show1 ? .blue : .gray)
                                    }.foregroundColor(show1 ? .blue : .gray)
                                    .padding()


                                    Button(action: {
                                        withAnimation(.spring())
                                        {
                                    self.show2.toggle()
                                    }
                                    }) {
                                            Text("View2")
                                            .font(.headline)
                                            .fontWeight(.regular)
                                            .lineLimit(nil)
                                            .padding(EdgeInsets.init(top: -5, leading: 15, bottom: 0, trailing: 15))
                                        .foregroundColor(show2 ? .blue : .gray)

                                    }.foregroundColor(show2 ? .blue : .gray)
                                    .padding()


                                    Button(action: {
                                          withAnimation(.spring())
                                          {
                                      self.show3.toggle()
                                      }
                                      }) {
                                        Text("View3")
                                            .font(.headline)
                                            .fontWeight(.regular)
                                            .lineLimit(nil)
                                            .padding(EdgeInsets.init(top: -5, leading: 15, bottom: 0, trailing: 10))
                                        .foregroundColor(show3 ? .blue : .gray)

                                    }.foregroundColor(show3 ? .blue : .gray)
                                .padding()
                                    }}}
                    }
                            VStack (alignment: .center) {
                    if self.show1 {
                        ShowView1().transition(
                            AnyTransition.move(edge: .bottom).combined(with: .move(edge: .bottom)).combined(with: .opacity))
                            }
                            }

                    VStack (alignment: .center) {
                            if self.show2 {
                                 ShowView2().transition(
                                    AnyTransition.move(edge: .bottom).combined(with: .move(edge: .bottom)).combined(with: .opacity))
                            }
                    }

                    VStack (alignment: .center) {
                            if self.show3 {
                                 ShowView3().transition(
                                    AnyTransition.move(edge: .bottom).combined(with: .move(edge: .bottom)).combined(with: .opacity))
                            }
                    }}}}


struct ShowView1: View {
    var body: some View {
        ZStack() {
            Color.white
                .frame(maxWidth: .infinity, maxHeight: 52)
            }

        }
    }

struct ShowView2: View {
    var body: some View {
        ZStack() {
            Color.white
                .frame(maxWidth: .infinity, maxHeight: 600)

            }
        }
    }

struct ShowView3: View {
     var body: some View {
         ZStack() {
             Color.white
                 .frame(maxWidth: .infinity, maxHeight: 300)
             }
         }
     }

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

1 Ответ

1 голос
/ 26 февраля 2020

Вы фактически не переключаетесь, а переключаете каждое представление, поэтому в результате получаете сосуществующие представления. Здесь исправлена ​​часть кода (только переключение логи c). Протестировано с Xcode 11.2 / iOS 13.2.

struct ContentView: View {
    @State var selectedView: Int? = nil

    var body: some View {
        ZStack {
            NavigationView {
                VStack {
                    Section {
                        Color(red: 0.88, green: 0.88, blue: 0.88)
                            .frame(maxWidth: .infinity, maxHeight: .infinity).edgesIgnoringSafeArea(.all)
                        HStack {

                            Button(action: {
                                withAnimation(.spring())
                                {
                                    self.selectedView = 1
                                }
                            }) {
                                Text("View1")
                                    .font(.headline)
                                    .fontWeight(.regular)
                                    .lineLimit(nil)
                                    .padding(EdgeInsets.init(top: -5, leading: 15, bottom: 0, trailing: 15))
                                    .foregroundColor(self.selectedView == 1 ? .blue : .gray)
                            }.foregroundColor(self.selectedView == 1 ? .blue : .gray)
                                .padding()


                            Button(action: {
                                withAnimation(.spring())
                                {
                                    self.selectedView = 2
                                }
                            }) {
                                Text("View2")
                                    .font(.headline)
                                    .fontWeight(.regular)
                                    .lineLimit(nil)
                                    .padding(EdgeInsets.init(top: -5, leading: 15, bottom: 0, trailing: 15))
                                    .foregroundColor(self.selectedView == 2 ? .blue : .gray)

                            }.foregroundColor(self.selectedView == 2 ? .blue : .gray)
                                .padding()


                            Button(action: {
                                withAnimation(.spring())
                                {
                                    self.selectedView = 3
                                }
                            }) {
                                Text("View3")
                                    .font(.headline)
                                    .fontWeight(.regular)
                                    .lineLimit(nil)
                                    .padding(EdgeInsets.init(top: -5, leading: 15, bottom: 0, trailing: 10))
                                    .foregroundColor(self.selectedView == 3 ? .blue : .gray)

                            }.foregroundColor(self.selectedView == 3 ? .blue : .gray)
                                .padding()
                        }}}
            }
            VStack (alignment: .center) {
                if self.selectedView == 1 {
                    ShowView1().transition(
                        AnyTransition.move(edge: .bottom).combined(with: .move(edge: .bottom)).combined(with: .opacity))
                }

                if self.selectedView == 2 {
                    ShowView2().transition(
                        AnyTransition.move(edge: .bottom).combined(with: .move(edge: .bottom)).combined(with: .opacity))
                }

                if self.selectedView == 3 {
                    ShowView3().transition(
                        AnyTransition.move(edge: .bottom).combined(with: .move(edge: .bottom)).combined(with: .opacity))
                }
            }}}}
...