【发布时间】:2021-03-03 10:06:51
【问题描述】:
我正在使用选择多个缩略图块的功能处理我的项目。只有选定的缩略图/图像会突出显示。 对于 ChildView,如果用户点击图像,则绑定 activeBlock 应设置为 true/false。 但是,当我选择缩略图时,所有缩略图都会突出显示。我想出了一些想法,例如
@State var selectedBlocks:[Bool]
// which should contain wether or not a certain block is selected.
但我不知道如何实现。
这是我的代码:
子视图
@Binding var activeBlock:Bool
var thumbnail: String
var body: some View {
VStack {
ZStack {
Image(thumbnail)
.resizable()
.frame(width: 80, height: 80)
.background(Color.black)
.cornerRadius(10)
if activeBlock {
RoundedRectangle(cornerRadius: 10)
.stroke(style: StrokeStyle(lineWidth: 2))
.frame(width: 80, height: 80)
.foregroundColor(Color("orange"))
}
}
}
块视图
struct VideoData: Identifiable{
var id = UUID()
var thumbnails: String
}
struct BlockView: View {
var videos:[VideoData] = [
VideoData(thumbnails: "test"), VideoData(thumbnails: "test2"), VideoData(thumbnails: "test1")
]
@State var activeBlock = false
var body: some View {
ScrollView(.horizontal){
HStack {
ForEach(0..<videos.count) { _ in
Button(action: {
self.activeBlock.toggle()
}, label: {
ChildView(activeBlock: $activeBlock, thumbnail: "test")
})
}
}
}
}
感谢您的帮助!
【问题讨论】:
标签: foreach binding swiftui multipleselection