【问题标题】:SwiftUI Infinite loops inside picker选择器内的 SwiftUI 无限循环
【发布时间】:2019-11-24 19:25:56
【问题描述】:

尝试执行函数但返回“某些视图”时遇到问题。我也在尝试使用 continue 语句执行 while 循环,但这在 viewBuilder 的主体中是不允许的...当条件发生变化时使用选择器的最简单方法是什么?

var body: some View {
    VStack{
        Group{
            Form {
                Section(header: Text("\(screeningTable())")) {
                    Picker(selection: $updateMaleBodyCompView.age, label: Text("Select age")) {
                        List(maleDataModel.ageArray, id: \.self) { i in
                            Text("\(i, specifier: "%g")-years-old")
                        }
                    }

                if updateMaleBodyCompView.age == 0 || updateMaleBodyCompView.height == 0 || updateMaleBodyCompView.weight == 0 {
                    Section {
                        Text("Fill all required fields").foregroundColor(.red)
                    }
                }

                if screeningTable() == "No Go" {
                    Section(header: Text("Tape Mesurements")) {
                        MaleTapeView()
                    }
                }
                if updateMaleBodyCompView.age != 0 && updateMaleBodyCompView.height != 0 && updateMaleBodyCompView.weight != 0 && screeningTable() != "No Go"{
                    Section(header: Text("You are not required to tape").foregroundColor(.blue)) {
                        MaleSaveButton()
                    }
                }
            }
        }

【问题讨论】:

标签: conditional-statements swiftui picker


【解决方案1】:

我不确定您的代码是否有效,但如果您想要 if 语句,可以将它们包含在 ZStacks 中

var body: some View {
    VStack{
        Group{
            Form {
                Section(header: Text("\(screeningTable())")) {
                    Picker(selection: $updateMaleBodyCompView.age, label: Text("Select age")) {
                        List(maleDataModel.ageArray, id: \.self) { i in
                            Text("\(i, specifier: "%g")-years-old")
                        }
                    }
                ZStack {
                    if updateMaleBodyCompView.age == 0 || updateMaleBodyCompView.height == 0 || updateMaleBodyCompView.weight == 0 {
                        Section {
                            Text("Fill all required fields").foregroundColor(.red)
                        }
                    }
                }
                ZStack {
                    if screeningTable() == "No Go" {
                        Section(header: Text("Tape Mesurements")) {
                            MaleTapeView()
                        }
                    }
                }
                ZStack {
                    if updateMaleBodyCompView.age != 0 && updateMaleBodyCompView.height != 0 && updateMaleBodyCompView.weight != 0 && screeningTable() != "No Go"{
                        Section(header: Text("You are not required to tape").foregroundColor(.blue)) {
                        MaleSaveButton()
                    }
                }
            }
        }
    }

【讨论】:

    【解决方案2】:

    我从设计 + 代码课程中得到了这个。我可以看到@State 属性如何在没有无穷无尽的条件语句的情况下来回切换......

                BottomCardView(showRing: $showCard)
                .offset(y: showCard ? screen.height/2 - 50 : screen.height)
                .offset(y: bottomState.height)
                .blur(radius: show ? 20 : 0)
                .animation(.spring(response: 0.5, dampingFraction: 0.7, blendDuration: 0))
                .gesture(
                    DragGesture()
                        .onChanged { value in
                            self.bottomState = value.translation
                            if self.showFull {
                                self.bottomState.height += -300
                            }
                            if self.bottomState.height < -300 {
                                self.bottomState.height = -300
                            }
                    }
                    .onEnded { value in
                        if self.bottomState.height > 50 {
                            self.showCard = false
                        }
                        if (self.bottomState.height < -100 && !self.showFull) || (self.bottomState.height < -250 && self.showFull) {
                            self.bottomState.height = -300
                            self.showFull = true
                        } else {
                            self.bottomState = .zero
                            self.showFull = false
                        }
                    }
            )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多