UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SwiftUI repeatedly fires up an animation which ought to be initiated once by property manipulation from within .onAppear(perform:)

Forums > SwiftUI

@EP2nd  

Hi, everyone!

I hereby give up my prolonged lone struggle to comprehend how to handle this unwanted in this situation SwiftUI behavior which has led me no further than to consolidating what I know and exploring what I haven't practiced enough yet, which is still great. Yet the ongoing lack of a feasible solution is really frustrating. And I humbly ask for your help.

I have an Item object that consists of properties like isItemNew and amount. Whenever the user adds a new item, it shows up in List. I have a cell text animation set up to occur once a new Item cell appears; it works by changing the values of my @State properties, offset and letterCount, from within .onAppear(perform:). After the change, once the animation finishes, isItemNew gets toggled with the help of asyncAfter(deadline:execute:) to prevent animation from reoccuring.

The problem is the animation occurs every time SwiftUI reinvokes the view (closing and reopening the app, reloading Canvas in Xcode). If the user manipulated the amount property of a newly added item, the animation is never reinvoked again, and that's asbolutely wanted.

How can I make the compiler remember right after the first animation happens that it shouldn't animate anymore after isItemNew toggles to false? I have that condition coded in my implicit .restrictedAnimation custom view modifier, but the view is being refreshed along with the animation nontheless.

I've even created a custom .onFirstAppear view modifier, not to mention prior trials with less extensive conditional statements, but to no avail. I reckon this has to do with the way SwiftUI handles view invocation, but I'm not sure how to deal with it in this situation. I feel as if the solution is childlishly simple, and it slips through my fingers, but here I am...

Any help would be very much appreciated!

2      

Were you able to find a solution?

i tried creating a view model to save the state but it was keep running every time I changes tabs, or it didn't run at all

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.