【问题标题】:SwiftUI: (Bool variable in ForEach) What is wrong with my variable in ForEach?SwiftUI:(ForEach 中的布尔变量)我在 ForEach 中的变量有什么问题?
【发布时间】:2020-08-26 01:47:52
【问题描述】:

我想使用 ForEach 为数组中的每个字符串创建 2 个布尔变量,但是当我按下按钮时布尔变量不起作用,我该怎么办? (使用此代码我想制作一个按钮,当按下它时,会显示更多带有过渡的信息,我想要每个变量有两个)

ForEach(0 ..< Array.count, id:\.self){ ArrayCount in
                                
                                let Title = Array.count[ArrayCount]
                                let Description = UserDefaults.standard.string(forKey: Title+"Description") ?? ""
                                
                                var ShowTip = false
                                var PressAnim = false
                                
                                    VStack(alignment: .center){
                                                Button(action:{
                                                    withAnimation {
                                                        ShowTip.toggle()
                                                        PressAnim.toggle()
                                                    }
                                                }){
                                                    ZStack
                                                    {
                                                        RoundedRectangle(cornerRadius: 20)
                                                            .foregroundColor(.clear)
                                                            .background(Color(UIColor.systemGray4))
                                                            .cornerRadius(20)
                                                            .frame(width: WidthScreen)
                                                            .rotationEffect(.degrees(EditMode ? 2 : 0))
                                                            .animation(self.EditMode ? Animation.easeInOut(duration: 0.1).repeatForever(autoreverses: true).delay(0.1) : Animation.easeInOut(duration: 0))
                                                            .rotationEffect(.degrees(EditMode ? -2 : 0))
                                                            .animation(self.EditMode ? Animation.easeInOut(duration: 0.1).repeatForever(autoreverses: true).delay(0.2) : Animation.easeInOut(duration: 0))
                                                        
                                                        HStack{
                                                        Text(Title)
                                                            .font(.custom("Arial", size: 20))
                                                            .fontWeight(.bold)
                                                            .foregroundColor(Color(UIColor.label))
                                                            .frame(width: WidthScreen - 25, height: 50, alignment: .leading)
                                                            .offset(x: 10, y: 0)
                                                        
                                                        Image("Arrow2").resizable()
                                                            .renderingMode(.template)
                                                            .foregroundColor(Color(UIColor.label))
                                                            .rotationEffect(.degrees(PressAnim ? 90 : 0))
                                                            .frame(width:10, height: 19)
                                                            .offset(x: -10, y: 0)
                                                        }
                                                    }
                                                }
                                                .frame(width: WidthScreen, height: 50, alignment: .center)
                                                .fixedSize()
                                            if ShowTip{
                                                
                                                ZStack
                                                {
                                                    RoundedRectangle(cornerRadius: 15)
                                                        .foregroundColor(.clear)
                                                        .background(Color(UIColor.systemGray4))
                                                        .cornerRadius(15)
                                                        .frame(width: WidthScreen)
                                                    
                                                    HStack{
                                                        Text(Description)
                                                            .foregroundColor(Color(UIColor.label))
                                                            .padding(.all, 15)
                                                    }
                                                }
                                                .frame(width: WidthScreen, alignment: .center)
                                                .transition(.asymmetric(insertion: .scale, removal: .opacity))
                                                .fixedSize()
                                                }
                                    }
                            }

【问题讨论】:

  • 尝试将您的作用域变量 (ShowTip ..) 移出 ForEach

标签: swift foreach swiftui boolean show


【解决方案1】:

将它们移出正文上下文并使其成为视图的状态属性,例如

@State private var ShowTip = false
@State private var PressAnim = false

重要提示: 将变量命名为小写且有意义,例如,因为您的 Array 与标准 swift 类型 Array 冲突,因此后果可能无法预测。您的自定义类型也是如此 - 为它们指定唯一名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 2022-01-02
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多