If you are trying to coordinate multiple animations’ playback at runtime,
consider using a state machine instead to do this for you!
Choosing starting animations
Starting animations can also be chosen when Rive is instantiated. The first animation on the artboard may play if one is not provided, or a state machine is not set. By default,RiveViewModel will automatically play the animation or state machine you’ve given it.
SwiftUI
UIKit
Controlling playback
Playback of each animation and state machine can be separately controlled. You can play and pause playback using theplay , pause and stop methods, either passing in the names of the animations you want to affect or passing in nothing which will affect all instanced animations.
Invoking Playback Controls
After creating aRiveViewModel, you can invoke animation playback control methods on a reference to this view model.
Very often that will be all that is needed to display your Rive asset. However, we have some convenient controls for when you want more fine-grained control of when it plays and doesn’t.
You can also choose the loop mode of the animation as additional parameters as needed. Along with playing animations, you similarly have the ability to pause, stop, and reset animation(s).
Playing without
play(animationName: String? = nil, loop: Loop = .autoLoop, direction: Direction = .autoDirection)animationName- Name of the animation to playloop- Loop mode to play the animation in. Possible values listed below:oneShot- plays animation through onceloop- plays through animation and repeats from the set starting timepingPong- plays animation from start -> end, then end -> start on repeatautoLoop(default) - plays through the loop setting set on the animation
direction- Direction to play the animation inbackwards- plays through animation timeline backwardforwards- plays through animation timeline forwardsautoDirection- plays through direction set on the animation
pause()stop()reset()
Play
If you set autoplay to false you can play the active animation or state machine very simply.Pause/Stop/Reset
Based on certain events in your app you may want to adjust the playback further.Player Delegates
This runtime allows for delegates that can be set on theRiveViewModel. You can use delegates to define functions that hook into when certain playback events are invoked. See the below class for how you can hook into the following playback events:
- played
- paused
- stopped
- advanced
- animation looped