ios - SwiftUI:有没有办法在点击时只折叠一个按钮而不是全部

标签 ios swift swiftui

我在 ScrollView 中添加了三个按钮,我想在用户单击按钮时获得效果 - 仅单击该按钮而不是每个按钮,以便隐藏描述仅出现在单击的按钮上。

Buttons without description

Buttons with description

和代码:

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)
           }
        }
    }
}

最佳答案

这是一个解决方案。使用 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)
           }
        }
    }
}

关于ios - SwiftUI:有没有办法在点击时只折叠一个按钮而不是全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61933338/

相关文章:

ios - iOS 中根据 touchid 了解哪个用户登录了应用程序

ios - 从客户端应用程序写入不属于当前设备 PFInstallation iOS Swift 3 的解析安装类对象

ios - 使用 Swift 3 的 Alamofire 4.0 出现 "has no member"错误

swift - 如何在 swiftUI 中更改导航标题颜色?

swiftui - 有人可以使用 SwiftUI+MapKit+LongPress 吗?

ios - 为什么 NSFileManager 找不到这些 png 文件?

iphone - MPMoviePlayerController 在 iPhone 应用程序中反向(向后)播放电影不流畅

arrays - 将项目移动到集合开头的最佳方法

swift - 是否可以在 SwiftUI 中提供服务并将数据推送到 View 并更新 UI?

ios - 启动 Storyboard中的图像未更新