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

Проблема: у меня есть три вида, между которыми я переключаюсь, но после выбора каждого вида я не могу отменить выбор каждого вида и отклонить кадр.

View 1 Selected

Если View1 или любой другой вид был отменен после его выбора, он будет выглядеть следующим образом:

enter image description here

Вы можете скопировать и вставить код ниже в Xcode, чтобы увидеть, как он работает в данный момент:

import SwiftUI

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

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

Вы можете отменить выбор вида, изменив код внутри действия button's, изменив выбранное состояние на nil, если оно выбрано в данный момент.

self.selectedView == 1 ? (self.selectedView = nil) : (self.selectedView = 1)

Рабочий раствор:

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 ? (self.selectedView = nil) : (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 ? (self.selectedView = nil) : (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 ? (self.selectedView = nil) : (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))
            }
        }}}}
...