Use this file to discover all available pages before exploring further.
State machines enable interactive, logic-driven animations in Rive. They respond to user input, trigger events, and manage complex animation flows automatically.
The RiveStateMachineInstance class represents a running instance of a state machine from your Rive file. State machines control animation states based on inputs and conditions, making them ideal for interactive experiences.
do { let file = try RiveFile(name: "button") let artboard = try file.artboard() // Get state machine by name let sm = try artboard.stateMachine(fromName: "Button State") // Get state machine by index let firstSM = try artboard.stateMachine(from: 0) // Get default state machine if let defaultSM = artboard.defaultStateMachine() { print("Using default state machine") } // List available state machines let names = artboard.stateMachineNames() print("Available state machines: \(names)")} catch { print("Error loading state machine: \(error)")}
do { let model = try RiveModel(fileName: "button") // Set state machine by name try model.setStateMachine("Button State") // Set default state machine try model.setStateMachine() // Access the state machine instance let sm = model.stateMachine} catch { print("Error setting state machine: \(error)")}
// Get input countlet count = sm.inputCount()print("State machine has \(count) inputs")// List input nameslet names = sm.inputNames()print("Available inputs: \(names)")// Get input by namedo { let input = try sm.input(fromName: "Hover")} catch { print("Input not found")}// Get input by indexdo { let input = try sm.input(from: 0)} catch { print("Input not found")}
// Get boolean inputif let boolInput = sm.getBool("isActive") { // Read current value let currentValue = boolInput.value() // Set value boolInput.value(true)}
// Get number inputif let numberInput = sm.getNumber("Level") { // Read current value let currentValue = numberInput.value() // Set value numberInput.value(2.5)}
// Get number of state changes since last framelet changeCount = sm.stateChangedCount()if changeCount > 0 { // Get state changes by index for i in 0..<changeCount { if let state = try? sm.stateChanged(from: i) { print("Changed to state: \(state)") } }}// Get all state change nameslet changes = sm.stateChanges()print("States changed: \(changes)")
// Get number of reported eventslet eventCount = sm.reportedEventCount()// Process eventsfor i in 0..<eventCount { let event = sm.reportedEvent(at: i) // Handle the event print("Event fired: \(event.name())")}
let file = try RiveFile(name: "animation")let artboard = try file.artboard()let sm = try artboard.stateMachine(fromName: "State Machine 1")if let viewModel = file.defaultViewModel(for: artboard), let instance = viewModel.createDefaultInstance() { // Bind to state machine (automatically binds to artboard too) sm.bind(viewModelInstance: instance)}