You should add the animation as a modifier for LazyGrid.
Note: replacing num by id = UUID() simplifies a little bit.
I tested the following:
struct MyEmoji {
var char: String
var id = UUID()
}
struct ContentView: View {
@State var amount = 1.0
var emojis : [MyEmoji] = [MyEmoji(char: "🐶"), MyEmoji(char: "🐱"), MyEmoji(char: "🐱"), MyEmoji(char: "🦊"), MyEmoji(char: "🦁"), MyEmoji(char: "🐝"), MyEmoji(char: "🐼"), MyEmoji(char: "🐷"), MyEmoji(char: "🐮")]
var body: some View {
HStack {
Spacer()
Button(action: {
self.amount += 0.1
}) {
Text("More")
}
Spacer()
Button(action: {
self.amount -= 0.1
}) {
Text("Less")
}
Spacer()
}
LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {
ForEach(emojis, id: \.id, content: { emoji in
emojiView(content: emoji.char)
})
}
.scaleEffect(amount)
.animation(.easeInOut(duration: 3), value: amount)
// .animation(.spring(), value: amount) // another one
}
}
struct emojiView: View {
@State var isFaceUp: Bool = true
var content: String = ""
var body: some View {
ZStack {
let shape = RoundedRectangle(cornerRadius: 20.0)
if isFaceUp {
shape
.frame(width: 90, height: 120)
.foregroundColor(.yellow)
Text(content).font(.largeTitle)
} else {
shape.frame(width: 90, height: 120)
shape.fill()
}
}
.rotation3DEffect(Angle(degrees: isFaceUp ? 180 : 0), axis: (x: 0, y: 1, z: 0))
.animation(.easeIn(duration: 2), value: isFaceUp)
.onTapGesture { isFaceUp = !isFaceUp }
}
}
See other example here:
https://www.reddit.com/r/SwiftUI/comments/myvrmz/fun_stuff_with_lazyvgrid_and_spring_animation_for/