Ios SwiftUI:有没有一种方法可以在单击时只折叠一个按钮而不是全部按钮

Ios SwiftUI:有没有一种方法可以在单击时只折叠一个按钮而不是全部按钮,ios,swift,swiftui,Ios,Swift,Swiftui,我在scroll视图中添加了三个按钮,我希望在用户单击按钮时获得效果-只单击该按钮,而不是每个按钮,因此隐藏的描述仅显示在单击的按钮上 和代码: struct WordCells: View { @State private var toggleView = false var numberOfItems = Int.init() var body: some View { GeometryReader { geometry in

我在scroll视图中添加了三个按钮,我希望在用户单击按钮时获得效果-只单击该按钮,而不是每个按钮,因此隐藏的描述仅显示在单击的按钮上

和代码:

struct WordCells: View {
    @State private var toggleView = false
    var numberOfItems = Int.init()
    var body: some View {
        GeometryReader { geometry in
            VStack(spacing: 40.0) {
                ForEach(0..<self.numberOfItems) {item in
                    Button(action: {
                        withAnimation(.easeInOut(duration: 0.5)) {
                            self.toggleView.toggle()
                       }
                   })
                   {
                    VStack {
                       HStack {
                            Text("Button Text")
                                .fontWeight(.bold)
                                .foregroundColor(.black)
                                .font(.callout)

                            Spacer()

                            Text("Description")
                                .fontWeight(.bold)
                                .foregroundColor(.customLabel)
                                .font(.callout)
                            }
                        if self.toggleView {
                            HiddenDescriptionView()
                        }
                       }
                       .frame(width: geometry.size.width/1.3)
                   }

                   .padding(23.0)
                   .background(Color.white)

               }
                .clipShape(RoundedRectangle(cornerRadius: 32))
                .shadow(color: .customLabel, radius: 15)
           }
        }
    }
}
struct WordCells:视图{
@状态私有变量toggleView=false
var numberOfItems=Int.init()
var body:一些观点{
GeometryReader{中的几何体
VStack(间距:40.0){

ForEach(0..这里有一个解决方案。使用Xcode 11.4/iOS 13.4进行测试

struct WordCells: View {
    @State private var toggleView: Int? = nil  // << selection
    var numberOfItems = Int.init()
    var body: some View {
        GeometryReader { geometry in
            VStack(spacing: 40.0) {
                ForEach(0..<self.numberOfItems) {item in
                    Button(action: {
                        withAnimation(.easeInOut(duration: 0.5)) {
                            if self.toggleView == item { // << here !!
                                self.toggleView = nil
                            } else {
                                self.toggleView = item
                            }
                       }
                   })
                   {
                    VStack {
                       HStack {
                            Text("Button Text")
                                .fontWeight(.bold)
                                .foregroundColor(.black)
                                .font(.callout)

                            Spacer()

                            Text("Description")
                                .fontWeight(.bold)
                                .foregroundColor(.gray)
                                .font(.callout)
                            }
                        if self.toggleView == item { // << selection !!
                            HiddenDescriptionView()
                        }
                       }
                       .frame(width: geometry.size.width/1.3)
                   }

                   .padding(23.0)
                   .background(Color.white)

               }
                .clipShape(RoundedRectangle(cornerRadius: 32))
                .shadow(color: .gray, radius: 15)
           }
        }
    }
}
struct WordCells:视图{

@State private var toggleView:Int?=nil//您需要一个布尔值数组,以便跟踪每个按钮的扩展状态