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.
Create the state machine directly from an Artboard:
Copy
Ask AI
final artboard = riveFile.defaultArtboard()!;// Default state machinevar stateMachine = artboard.defaultStateMachine();// By namestateMachine = artboard.stateMachine('State Machine 1');// By indexstateMachine = artboard.stateMachineAt(0);
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):
Not supported. This is a legacy feature and we strongly recommend using Data Binding or Events instead.
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)