Progress Animation in SwiftUI
Implement a struct with single Rectangle with index parameter for each view to start and stop the animation programmatically via publisher with
PassthroughSubject
is passed as a parameter — which sends stream of values from their origin to it’ subscribers. ThePassthroughSubject's
subscribers can use this information to update the state of the UI or to handle the occurred event. In.onReceive
will have access to the value sent by the publisher to act on to update the UI state.
@State Variable animate
is set to true or false based on the value received from the publisher to control the opacity of the RectangleView
Next, create ProgressView( see the references link below) with @state currentIndex,publisher
variables. In it’s .onAppear
pass the value via publisher to the it’s subscriber RectangleView
in this case
PassthroughSubject
takes AnimationStatus
enum to start, stop at particular index and completely everything.
References : https://gist.github.com/badrinathvm/13b79f1d6dfd30ea54a106dc1d805953
Happy Coding !!