我想使用ForEach为数组中的每个字符串制作2个bool变量,但是按下按钮时bool变量不起作用,该怎么办? (使用此代码,我想创建一个按钮,当按下按钮时,显示带有过渡的更多信息,我希望每个变量都有两个变量)
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()
}
}
}
答案 0 :(得分:0)
将它们移出主体上下文,并使其成为视图的状态属性,例如
@State private var ShowTip = false
@State private var PressAnim = false
重要提示:确实将变量命名为小写且有意义,例如,由于您的Array
与标准的swift类型Array
冲突,因此后果可能无法预测。您的自定义类型也是如此-为它们指定唯一的名称。