RiveModel represents a Rive file and provides methods to access and configure artboards, animations, and state machines. It serves as the data layer between a Rive file and the view that renders it.
Initializers
Initialize with RiveFile
public init(riveFile: RiveFile)
An already loaded RiveFile instance
Example:
let riveFile = try RiveFile(name: "hero")
let model = RiveModel(riveFile: riveFile)
Initialize with File Name
public init(
fileName: String,
extension: String = ".riv",
in bundle: Bundle = .main,
loadCdn: Bool = true,
customLoader: LoadAsset? = nil
) throws
The name of the Rive file (without extension)
The bundle containing the Rive file
Whether to load assets from CDN
Custom asset loader for loading external assets
Example:
let model = try RiveModel(fileName: "hero")
Initialize with Web URL
public init(
webURL: String,
delegate: RiveFileDelegate,
loadCdn: Bool
)
The URL of the Rive file to load from the web
Delegate to receive callbacks when the file loads or errors
Whether to load assets from CDN
Example:
class MyDelegate: RiveFileDelegate {
func riveFileDidLoad(_ riveFile: RiveFile) throws {
print("File loaded")
}
func riveFileDidError(_ error: any Error) {
print("Error: \(error)")
}
}
let delegate = MyDelegate()
let model = RiveModel(webURL: "https://example.com/file.riv", delegate: delegate, loadCdn: true)
Properties
stateMachine
public internal(set) var stateMachine: RiveStateMachineInstance?
The currently active state machine instance. Read-only.
animation
public internal(set) var animation: RiveLinearAnimationInstance?
The currently active animation instance. Read-only.
artboard
public private(set) var artboard: RiveArtboard!
The currently active artboard. Read-only.
riveFile
public private(set) var riveFile: RiveFile
The underlying RiveFile instance. Read-only.
volume
The volume of the current artboard, if available. Defaults to 1.0. Setting this value will update the artboard’s volume if an artboard is configured.
Example:
model.volume = 0.5 // Set volume to 50%
Methods
setArtboard (by name)
open func setArtboard(_ name: String) throws
Sets a new Artboard and makes the current StateMachine and Animation nil.
The name of the artboard to set
Throws: RiveModelError.invalidArtboard if the artboard name is not found.
Example:
try model.setArtboard("Character")
setArtboard (by index or default)
open func setArtboard(_ index: Int? = nil) throws
Sets a new Artboard and makes the current StateMachine and Animation nil.
The index of the artboard to set. Pass nil to use the default artboard
Throws: RiveModelError.invalidArtboard if the artboard is not found.
Example:
// Use default artboard
try model.setArtboard()
// Use artboard at index 0
try model.setArtboard(0)
setStateMachine (by name)
open func setStateMachine(_ name: String) throws
Sets the active state machine by name.
The name of the state machine to set
Throws: RiveModelError.invalidStateMachine if the state machine name is not found.
Example:
try model.setStateMachine("State Machine 1")
setStateMachine (by index or default)
open func setStateMachine(_ index: Int? = nil) throws
Sets the active state machine by index or uses the default.
The index of the state machine to set. Pass nil to use the default state machine from the artboard, or index 0 as a fallback
Throws: RiveModelError.invalidStateMachine if the state machine is not found.
Example:
// Use default state machine
try model.setStateMachine()
// Use state machine at index 0
try model.setStateMachine(0)
setAnimation (by name)
open func setAnimation(_ name: String) throws
Sets the active animation by name.
The name of the animation to set
Throws: RiveModelError.invalidAnimation if the animation name is not found.
Example:
try model.setAnimation("idle")
setAnimation (by index or default)
open func setAnimation(_ index: Int? = nil) throws
Sets the active animation by index. Defaults to index 0 if no index is provided.
The index of the animation to set. Defaults to 0 if not provided
Throws: RiveModelError.invalidAnimation if the animation is not found.
Example:
// Use animation at index 0
try model.setAnimation()
// Use animation at index 1
try model.setAnimation(1)
enableAutoBind
open func enableAutoBind(_ callback: @escaping AutoBindCallback)
Automatically binds the default instance of the current artboard when the artboard and/or state machine changes, including when it is first set. The callback will be called with the instance that has been bound. A strong reference to the instance must be made in order to update properties and utilize observability.
The callback to be called when a RiveDataBindingViewModel.Instance is bound to the current artboard and/or state machine
Example:
model.enableAutoBind { instance in
// Store strong reference
self.viewModelInstance = instance
// Update properties
instance.propertyController.set("username", value: "John")
}
disableAutoBind
open func disableAutoBind()
Disables the auto-binding feature enabled by enableAutoBind.
Example:
Type Aliases
AutoBindCallback
public typealias AutoBindCallback = (RiveDataBindingViewModel.Instance) -> Void
Callback type for auto-binding data binding view model instances.
Errors
RiveModelError
enum RiveModelError: Error {
case invalidStateMachine(_ message: String)
case invalidAnimation(_ message: String)
case invalidArtboard(_ message: String)
}
Errors that can be thrown by RiveModel methods.
Usage Examples
Basic Model Setup
import RiveRuntime
// Create a model
let model = try RiveModel(fileName: "hero")
// Set artboard and animation
try model.setArtboard("MainArtboard")
try model.setAnimation("idle")
Working with State Machines
import RiveRuntime
let model = try RiveModel(fileName: "button")
// Set artboard and state machine
try model.setArtboard()
try model.setStateMachine("State Machine 1")
// Access state machine inputs
if let stateMachine = model.stateMachine {
let trigger = stateMachine.getTrigger("Press")
trigger.fire()
}
Loading from Web
import RiveRuntime
class MyController: RiveFileDelegate {
var model: RiveModel?
func loadAnimation() {
model = RiveModel(
webURL: "https://example.com/animation.riv",
delegate: self,
loadCdn: true
)
}
func riveFileDidLoad(_ riveFile: RiveFile) throws {
print("File loaded successfully")
try model?.setArtboard()
try model?.setAnimation()
}
func riveFileDidError(_ error: any Error) {
print("Error loading file: \(error)")
}
}
Volume Control
import RiveRuntime
let model = try RiveModel(fileName: "audio_demo")
try model.setArtboard()
// Set volume to 50%
model.volume = 0.5
// Mute
model.volume = 0.0
// Full volume
model.volume = 1.0
Data Binding
import RiveRuntime
class MyViewController: UIViewController {
var model: RiveModel?
var viewModelInstance: RiveDataBindingViewModel.Instance?
func setupModel() {
model = try? RiveModel(fileName: "data_binding_demo")
model?.enableAutoBind { [weak self] instance in
self?.viewModelInstance = instance
// Set initial values
instance.propertyController.set("username", value: "Guest")
instance.propertyController.set("score", value: 0)
}
try? model?.setArtboard()
try? model?.setStateMachine()
}
func updateScore(_ newScore: Int) {
viewModelInstance?.propertyController.set("score", value: newScore)
}
}