How to stop a view from animating on appear in SwiftUI?

Viewed 1018

I am animating a view in SwiftUI and it animates right when the view appears even if I don't have it in the .onAppear() method. I want it to animate only when I press the Text, that's why I am using the tap gesture. Here is my code:

struct ContentView: View {
     var body: some View {
          Text()
             .scaleEffect(cardTap ? 0.9 : 1)
                .gesture(LongPressGesture().onChanged { value in
                    self.cardTap = true
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                        self.cardTap = false
                        UIImpactFeedbackGenerator(style: .soft).impactOccurred()
                    }
                    }
            ).animation(.spring(response: 0.5, dampingFraction: 0.5, blendDuration: 0))
     }
}
1 Answers

You can limit animation to be triggered by specific value only, like in below example

Text()
 .scaleEffect(cardTap ? 0.9 : 1)
    .gesture(LongPressGesture().onChanged { value in
        self.cardTap = true
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            self.cardTap = false
            UIImpactFeedbackGenerator(style: .soft).impactOccurred()
        }
        }
)
.animation(.spring(response: 0.5, 
    dampingFraction: 0.5, blendDuration: 0), value: cardTap)  // << here !!
Related