For more information on designing and building state machines in Rive, please refer to: State Machine.
Rive’s state machines provide a way to combine a set of animation states and manage the transition between them that can be programmatically controlled with Data Binding (recommended) and Inputs.
In the above snippet, at instantiation time, the runtime will create a StateMachineController reference implicitly and immediately play the state machine.
If you’d like further control over the state machine in cases where you may want to prevent autoplaying the state machine, you can instead pass your own reference to a StateMachineController to the RiveAnimation widget at instantiation time with the isActive property set to false. Below is an example of what this might look like:
As you change the isActive property of the StateMachineController, you’ll see that the current state may pause advancing through the render loop. This is useful in cases where you may want to load in your Rive on screen, but delay playing until a loading sequence occurs, or some data comes back for your application.
Additionally, you can use the same APIs from animation playback (i.e play, pause, and stop) to control state machine playback, as long as you set the isStateMachine attribute to true.
We can set a callback to determine when the state machine changes state. onStateChange provides an event parameter that gives us the string name(s) of the current state(s):
We can set a callback to determine when the state machine changes state. onStateChange provides an event parameter that gives us the string name(s) of the current state(s):
If you’d like to know which state a state machine is in, or when a state machine transitions to another state, you can provide a callback to StateMachineController. The callback has the name of the state machine and the name of the animation associated with the state transitioned to:
This runtime allows for delegates that can be set on the RiveViewModel. If provided, these delegate functions will be fired whenever a matching event is triggered to be able to hook into and listen for certain events in the Rive animation cycle.
Currently, there exist the following delegates:
RivePlayerDelegate - Hook into animation and state machine lifecycle events
You can create your own delegate or mix in with the RiveViewModel, implementing as many protocols as are needed. Below is an example of how to customize a RiveViewModel’s implementation of the RivePlayerDelegate:
Copy
Ask AI
class SimpleAnimation: RiveViewModel { init() { let model = RiveModel(fileName: "truck_v7", stateMachineName: "Drive") super.init(model) } override func setView(rview view: RiveView) { super.setView(view) rview?.playerDelegate = self rview?.stateMachineDelegate = self } override func player(playedWithModel riveModel: RiveModel?) { if let stateMachineName = riveModel?.stateMachine?.name() {...} } override func player(pausedWithModel riveModel: RiveModel?) { if let stateMachineName = riveModel?.stateMachine?.name() {...} } override func player(stoppedWithModel riveModel: RiveModel?) { if let stateMachineName = riveModel?.stateMachine?.name() {...} } @objc func stateMachine(_ stateMachine: RiveStateMachineInstance, didChangeState stateName: String) { var stateMachineNames: [String] = [] var stateMachineStates: [String] = [] stateMachineNames.append(stateMachine.name()) stateMachineStates.append(stateName) ... }}
To listen for state changes, when creating a Listener to register on your animation view, you can add the following callback, where you’ll receive the name of the state machine, and the state it transitions to:
Copy
Ask AI
val listener = object : Listener { override fun notifyStateChanged(stateMachineName: String, stateName: String) { // Do something }}animationView.registerListener(listener)