在開發我的iOS應用程式時,我遇到了一個問題,即在UIImageViews陣列中獲取圖片以對該特定圖片執行操作。無論我對LazyVGrid照片集合中的哪張圖片進行LongPressGesture操作,它總是適用于顯示的第一張圖片。我認為這可能是我的陣列中的id的問題,ForEach回圈沒有正確識別每個元素,但是我不知道如何解決這個問題。
我把我的陣列放在ProfileViewModel類中:
@Published var userPicturesView。[UIImageView] = [UIImageView] ()
而我在我的ProfileView中回圈使用它:
ScrollView() {
LazyVGrid( columns: Array(repeating: GridItem(), count: 3) {
ForEach(0.<profileViewModel.userPicturesView.count, id: .self) { imageIndexin
if profileViewModel.userPicturesView[imageIndex].image != nil {
Image(uiImage: profileViewModel.userPicturesView[imageIndex].image! )
.可調整大小()
.border(Color.black, width: 0.25)
.frame(width: screenWidth * 0.34, height: screenHeight * 0.17)
.onLongPressGesture {
withAnimation {
self.shouldPresentEditActionSheet = true
}
}
.actionSheet(isPresented: $shouldPresentEditActionSheet) {
ActionSheet(標題。Text("Edit selected"), message: nil, buttons: [
.default(Text("Set as profile picture"), action: {
withAnimation {
self.profilePictureImage = Image(uiImage: self.profileViewModel.userPicturesView[imageIndex].image!)
}
}),
.destructive(Text("洗掉此照片"), action: {
withAnimation {
self.profileViewModel.deleteUserImage(imageIndex: imageIndex)
}
}),
.取消()
])
}
}
}
}
}
我可以補充一下,我試著通過添加容納我的圖片的自定義結構來解決這個問題:
struct PictureView: Identifiable {
var id = UUID()
var uiImageView。UIImageView()
init(uiImageView: UIImageView) {
self.uiImageView = uiImageView
}
}
然后像這樣在ProfileViewModel中制作一個此結構的陣列:
var userPicturesView: [PictureView] = [PictureView]()
然后以這種方式改變ProfileView:
。ScrollView() {
LazyVGrid( columns: Array(repeating: GridItem(), count: 3) {
ForEach(self.profileViewModel.userPicturesView) { userPictureView in
if userPictureView.uiImageView.image != nil {
Image(uiImage: userPictureView.uiImageView.image!/span>)
.可調整大小()
.border(Color.black, width: 0.25)
.frame(width: screenWidth * 0.34, height: screenHeight * 0.17)
.onLongPressGesture {
withAnimation {
self.shouldPresentEditActionSheet = true
}
}
.actionSheet(isPresented: $shouldPresentEditActionSheet) {
ActionSheet(標題。Text("Edit selected"), message: nil, buttons: [
.default(Text("Set as profile picture"), action: {
withAnimation {
self.profilePictureImage = Image(uiImage: userPictureView.uiImageView.image! /span>)
}
}),
.destructive(Text("delete this photo"), action: {
withAnimation {
//self.profileViewModel.deleteUserImage(imageIndex: imageIndex)。
}
}),
.取消()
])
}
}
}
}
}
但這帶來了同樣的結果。 感謝每一個幫助。
uj5u.com熱心網友回復:
使用actionSheet(item:)而不是actionSheet(isPresented:)將使你能夠訪問所選的專案。在iOS 14 中,isPresented存在著先渲染前一個值的問題。以下是你的代碼的簡化版本:
struct ContentView。View {
let pictureViews : [PictureView] = [ ]
@State private var selectedItem : PictureView? = nil?
var body。some View {
ForEach(pictureViews) { pictureView in
Image(uiImage: pictureView.uiImageView.image! )
.onLongPressGesture {
selectedItem = pictureView
}
}.actionSheet(item: $selectedItem) { item in.
ActionSheet(標題。Text("Edit selected"), message: nil, buttons: [
.default(Text("Set as profile picture"), action: {
withAnimation {
//在這里使用`item`。
}
}),
.destructive(Text("洗掉此照片"), action: {
withAnimation {
//在這里使用`item`。
}
}),
.取消()
])
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/307575.html
標籤:
