Swiftpack.co - rive-app/rive-ios as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by rive-app.
rive-app/rive-ios pre-high-level
iOS runtime for Rive
⭐️ 60
🕓 9 weeks ago
iOS
.package(url: "https://github.com/rive-app/rive-ios.git", from: "pre-high-level")

Build Status Discord badge Twitter handle

rive-ios

iOS runtime for Rive

Further runtime documentation can be found in Rive's help center.

Create and ship interactive animations to any platform

Rive is a real-time interactive design and animation tool. Use our collaborative editor to create motion graphics that respond to different states and user inputs. Then load your animations into apps, games, and websites with our lightweight open-source runtimes.

Beta Release

This is the iOS runtime for Rive, currently in beta. The api is subject to change as we continue to improve it. Please file issues and PRs for anything busted, missing, or just wrong.

Installing rive-ios

Via Cocoapods

To install our pod, simply add the following to cocoapods and run pod install.

pod 'RiveRuntime'

Once you have installed the pod, you can run

import RiveRuntime

to have access to our higher level views or build on top of our bindings to control your own animation loop.

Via Swift Package Manager

To install via Swift Package Manager, in the package finder in xcode, search with the Github repository name: https://github.com/rive-app/rive-ios

Examples

There is an example project next to the runtimes.

The examples show simple ways of adding animated views into your app, how to add buttons & slider controls, how to use state machines & how to navigate the contents of a rive file programatically.

To run the example, open the Rive.xcworkspace in Xcode and run the RiveExample project. Check out the Contributing docs to get set up.

Overview

We have provided high level Swift controller and a UIkit view to easily add Rive into your application. All of this is built ontop of an objective c layer that allows for fine grained granular animation control.

UIKit

RiveViewModel

The simplest way of adding Rive to a controller is to make a RiveViewModel and set its view as the RiveView when it is loaded.

class SimpleAnimationViewController: UIViewController {
    @IBOutlet weak var rview: RiveView!
    
    // Load the truck_v7 resource assets
    var rSimpleVM: RiveViewModel = RiveModel(fileName: "truck_v7")

    override public func viewDidLoad() {
        super.viewDidLoad()
        rSimpleVM.setView(rview)
    }
}

Rive will autoplay the first animation found in the Rive file passed in. You can also set the Rive file via a URL like so:

class SimpleAnimationViewController: UIViewController {
    @IBOutlet weak var rview: RiveView!
    var rSimpleVM: RiveViewModel = RiveModel(webURL: "https://cdn.rive.app/animations/vehicles.riv")

    override public func viewDidLoad() {
        super.viewDidLoad()
        rSimpleVM.setView(rview)
    }
}

The RiveViewModel can be further customized to select which animation to play, or how to fit the animation into the view space.

Layout

The Rive view can be further customized as part of specifying layout attributes.

fit can be specified to determine how the animation should be resized to fit its container. The available choices are:

  • .fitFill
  • .fitContain
  • .fitCover
  • .fitFitWidth
  • .fitFitHeight
  • .fitNone
  • .fitScaleDown

alignment informs how it should be aligned within the container. The available choices are:

  • alignmentTopLeft
  • alignmentTopCenter
  • alignmentTopRight
  • alignmentCenterLeft
  • alignmentCenter
  • alignmentCenterRight
  • alignmentBottomLeft
  • alignmentBottomCenter
  • alignmentBottomRight

By default, if no fit or alignment properties are set on the RiveViewModel, the view will be set with .fitContain and .alignmentCenter.

To understand more on these options, check out the help docs here.

To add layout options, you can set it below like:

let rSimpleVM = RiveModel(
    fileName: "truck_v7", 
    fit: .fitFill,
    alignment: .alignmentBottomLeft
)

or anytime afterwards.

rSimpleVM.fit = .fitCover
rSimpleVM.alignment = .alignmentCenter

Playback Controls

Animations can be controlled in many ways. Again by default, loading a RiveView will autoplay the first animation on the first artboard. The artboard and animation can be specified by name however if there are multiple artboards and/or animations defined in the Rive file.

let rMultiVM = RiveModel(
    riveFile: "artboard_animations",
    fit: .fitContain,
    alignment: .alignmentCenter,
    artboardName: "Square",
    animationName: "rollaround",
    autoplay: true
)

Furthermore animations can be controlled later too:

To play an animation named "rollaround":

rMultiVM.play(animationName: "rollaround")

Multiple animations can play at the same time, and additional animations can be added at any time:

rMultiVM.play(
    animationNames: ["bouncing", "windshield_wipers"]
)

When playing animations, the loop mode and direction of the animations can also be set:

rMultiVM.play(
    animationNames: ["bouncing", "windshield_wipers"],
    loop: .loopOneShot,
    direction: .directionBackwards
)

Similarly, animations can be paused or stopped either all at the same time or one by one.

rMultiVM.stop()
rMultiVM.stop(animationName:"bouncing")
rMultiVM.stop(animationNames:["bouncing", "windshield_wipers"])
rMultiVM.pause()
rMultiVM.pause(animationName:"bouncing")
rMultiVM.pause(animationNames:["bouncing", "windshield_wipers"])

Mixing

Mixing goes further than just playing multiple animations at the same time, animations can use a mix factor between 0 and 1, to allow multiple animations effects to blend together. The high level views do not expose this currently. but you can wrap your own render loop around the core libraries. The advance function is where you can specify a mix factor.

Delegates & Events

The rive-ios runtime allows for delegates that can be set on the RiveViewModel. If provided, these delegates 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 lifecycle events
    • loop: (animation animationName: String, type: Int) {}
    • play: (animation animationName: String, isStateMachine: Bool) {}
    • pause: (animation animationName: String, isStateMachine: Int) {}
    • stop: (animation animationName: String, isStateMachine: Int) {}
  • RiveStateDelegate - Hook into state changes on a state machine lifecycle
    • stateChange: (_ stateMachineName: String, _ stateName: String) {}
  • RiveInputDelegate - Hook into changes to available input states
    • inputs: (_ inputs: [StateMachineInput]) {}

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:

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
    }

    override func loop(animation animationName: String, type: Int) {
        // do things when the animation loops playing.
    }
    
    override func play(animation animationName: String, isStateMachine: Bool) {
        // do things when the animation starts playing.
    }

    override func pause(animation animationName: String, isStateMachine: Bool) {
        // do things when the animation is paused.
    }
    
    override func stop(animation animationName: String, isStateMachine: Bool) {
        // do things when the animation is stopped.
    }
}

Then you would instantiate your view model and configure it with the RiveView as you normally would:

class SimpleAnimationViewController: UIViewController {
    @IBOutlet weak var rview: RiveView!
    var rAnimationVM: RiveViewModel = SimpleAnimation()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        rAnimationVM.setView(rview)
    }
}

Blend modes

Rive allows the artist to set blend modes on shapes to determine how they are to be merged with the rest of the animation.

Each runtime is supporting the various blend modes natively, this means that there are some discrepancies in how blend modes end up being applied, we have a test file that is shipped inside the application that highlights the differences.

For ios, hue and saturation blend modes do not match the original.

Original iOS
Source iOS

Developing rive-ios

please see CONTRIBUTING.md for information on how to get setup and running with developing rive-ios

GitHub

link
Stars: 60
Last commit: 3 days ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

2.0.12
5 days ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics