作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 ScrollView 中添加了三个按钮,我想在用户单击按钮时获得效果 - 仅单击该按钮而不是每个按钮,以便隐藏描述仅出现在单击的按钮上。
和代码:
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/
我是一名优秀的程序员,十分优秀!