问题描述
我正在为应用程序创建可重复使用的图库视图,并且在点击任何图片时遇到困难,它假设会变成全屏,但无论点击图片如何,每次都只显示数组中的第一张图片。下面是我的代码,谢谢。
导入 SwiftUI
struct ReusablegalleryView:查看{ 让 greenappData:GreenAppNews
let gridLayout: [GridItem] = Array(repeating: GridItem(.flexible()),count: 3)
@State private var fullscreen = false
@State private var isPresented = false
var body: some View {
vstack{
ScrollView{
LazyVGrid(columns: gridLayout,spacing: 3) {
ForEach(greenappData.GreenAppgallery,id: \.self) { item in
Image(item)
.resizable()
.frame(width: UIScreen.main.bounds.width/3,height: 150)
.onTapGesture {
self.isPresented.toggle()
print(" tapping number")
}
.fullScreenCover(isPresented: $isPresented) {
FullScreenModalView( imageFiller: item)
}
.background(Color.gray.opacity(0.5))
}
}
.padding(.horizontal)
}
}
}
}
这是一个json数据的例子:
{ "id" : "1",“绿色应用画廊”:[ “图片1”, "图2","图 3","图 4","图5","图 6","图7","图 8","图 9",“图10” ] },
解决方法
fullScreenCover
,如 sheet
倾向于在 iOS 14 中使用 isPresented:
时创建这种类型的行为。
要修复它,您可以更改为 fullScreenCover(item: )
表单。
没有您的所有代码,我无法为您提供确切版本的代码,但要点是:
- 删除您的
isPresented
变量 - 将其替换为可选的
presentedItem
变量。可能是您图库中的数据类型。请注意,它必须符合Identifiable
(意味着它必须具有id
属性)。 - 不要切换
isPresented
,而是将presentedItem
设置为item
- 使用
fullScreenCover(item: ) { presentedItem in FullScreenModalView( imageFiller: presentedItem) }
并将您的presentedItem
变量传递给它 - 移动
fullScreenCover
使其附加到ForEach
循环而不是Image
使用此系统,您应该会看到它响应正确的项目。
这是我的另一个回答,其中包含 sheet
:@State var not updated as expected in LazyVGrid