gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:12:18 24 4
gpt4 key购买 nike

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com