As the code below, the data will not call deinit
method if uncomment the two onTapGesture modifier.
It works well when only active onTapGestrue
or matchedGeometryEffect
modifier, but seems to get memory leak when active both modifiers.
import SwiftUI
struct ContentView: View {
@Namespace private var gridNameSpace
@StateObject private var data = DataModel()
let gridItem = GridItem(.adaptive(minimum: 200, maximum: 350), spacing: 20)
var body: some View {
ScrollView {
LazyVGrid(columns: [gridItem], spacing: 20) {
ForEach(data.items) { item in
AnimalImage(image: item.image, favorite: item.isFavorite)
// .onTapGesture(count: 2) { toggleFavoriteStatus(item) }
// .onTapGesture(count: 1) { openModal(item, fromGrid: true) }
.matchedGeometryEffect(id: item.id, in: gridNameSpace, isSource: true)
}
}
}
}
func openModal(_ item: ItemData, fromGrid: Bool) {
withAnimation(.basic) {
// do somethings
}
}
func toggleFavoriteStatus(_ item: ItemData) {
if let idx = data.favorites.firstIndex(where: { $0.id == item.id }) {
item.isFavorite = false
withAnimation(.basic) {
// do somethings
}
} else {
withAnimation(.basic) {
// do somethings
}
}
}
}