【发布时间】: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