RiveViewModel and its view builder methods. This guide covers everything from basic setup to advanced techniques.
Quick Start
The simplest way to display a Rive animation in SwiftUI is to create aRiveViewModel and call its view() method:
Using @StateObject
For more control over your animation, store the view model as a@StateObject:
- Control playback (play, pause, stop)
- Trigger state machine inputs
- Respond to animation events
- Update text runs dynamically
Controlling Animations
Basic Playback Controls
Working with State Machines
Layout and Sizing
Fit and Alignment
Available Fit Options
.contain- Scales the animation to fit within the view while maintaining aspect ratio.fill- Fills the entire view, potentially cropping the animation.cover- Scales to cover the view while maintaining aspect ratio.fitWidth- Scales to match the view’s width.fitHeight- Scales to match the view’s height.scaleDown- Only scales down if the animation is larger than the view.layout- Resizes the artboard to match the view size.noFit- No scaling applied
Available Alignment Options
.topLeft,.topCenter,.topRight.centerLeft,.center,.centerRight.bottomLeft,.bottomCenter,.bottomRight
Advanced Features
Custom Layout Scale Factor
When using.layout fit mode, you can specify a custom scale factor:
Volume Control
Control audio volume for animations with sound:State Machine Inputs
Trigger Inputs
Boolean Inputs
Number Inputs
Reading Input Values
Nested Artboards
For nested artboards, specify the path to the input:Performance Optimization
Frame Rate Control
Best Practices
- Use @StateObject - Always use
@StateObjectfor view models to ensure proper lifecycle management - Reuse View Models - Create view models once and reuse them rather than recreating on every render
- Control AutoPlay - Set
autoPlay: falsewhen you need manual control over playback - Specify State Machines - Explicitly name the state machine or animation you want to use
- Handle Loading States - Consider showing a placeholder while animations load from URLs
Next Steps
- Loading Assets - Learn how to load .riv files from different sources
- Touch Events - Handle user interactions with your animations
- RiveViewModel API - Complete API documentation