跳到主要内容

数据绑定

数据绑定允许你将 Rive 文件中在编辑器里定义的属性连接到运行时代码。通过视图模型(View Model)和视图模型实例(View Model Instance),你可以读取、写入和监听字符串、数字、布尔值、颜色、枚举、触发器、图片、字体、Blob、列表和 Artboard 等绑定属性。

数据绑定的基本流程通常是:

  1. 在 Rive 编辑器中定义视图模型和属性。
  2. 在运行时从 File 中获取或引用视图模型。
  3. 创建一个视图模型实例。
  4. 将该实例绑定到 Artboard 或状态机。
  5. 在代码中读取、写入或监听实例上的属性。

视图模型​

视图模型定义了一组可绑定属性。它类似于一个数据结构或接口,用于描述运行时可以访问哪些数据。视图模型本身通常不是你直接操作的运行时数据对象;真正保存值的是视图模型实例。

当前 Apple Runtime​

视图模型不是单独的类型;更准确地说,它是在从 File 创建视图模型实例时使用的来源。

你可以通过 ViewModelSource 类型定义视图模型的来源。

case artboardDefault(Artboard) // References the default view model for an Artboard
case name(String) // References a view model from a file by name

这些来源会与获取视图模型实例的 API 一起使用。更多信息请参见视图模型实例。

Legacy Apple Runtime​

let riveViewModel = RiveViewModel(...)
let file = riveViewModel.riveModel!.riveFile

// Data binding view model by name
let viewModelByName = file.viewModelNamed("...")

// Data binding view model by index
for index in 0..<file.viewModelCount {
let viewModelByIndex = file.viewModel(at: index)
}

// Default data binding view model for an artboard
let artboard = riveViewModel.riveModel!.artboard
let viewModelForArtboard = file.viewModel(for: artboard)

视图模型实例​

视图模型实例是视图模型在运行时的具体数据对象。你会从实例中读取属性值、写入属性值、触发 trigger,或者监听属性变化。

一个视图模型可以有多个实例,例如空实例、默认实例,或在 Rive 文件中命名的实例。

当前 Apple Runtime​

以下内容假设你已经阅读过 Apple 概览。

// From a file
let file: File = ...

// When using a view model by name:
// A blank view model instance
var blankInstance = try await file.createViewModelInstance(.blank(from: .name("ViewModel")))
// The default instance for the view model
var defaultInstance = try await file.createViewModelInstance(.viewModelDefault(from: .name("ViewModel")))
// An instance by name from the view model
var namedInstance = try await file.createViewModelInstance(.name("Instance", from: .name("ViewModel")))

// Alternatively, using the default view model for an artboard
let artboard: Artboard = ...
// A blank view model instance
blankInstance = try await file.createViewModelInstance(.blank(from: .artboardDefault(artboard)))
// The default instance for the view model
defaultInstance = try await file.createViewModelInstance(.viewModelDefault(from: .artboardDefault(artboard)))
// An instance by name from the view model
namedInstance = try await file.createViewModelInstance(.name("Instance", from: .artboardDefault(artboard)))

Legacy Apple Runtime​

let riveViewModel = RiveViewModel(...)
let viewModel = riveViewModel.riveModel!.riveFile.viewModelNamed("...")!

// Create blank
let blankInstance = viewModel.createInstance()

// Create default
let defaultInstance = viewModel.createDefaultInstance()

// Create by index
for index in 0..<viewModel.instanceCount {
let instanceByIndex = viewModel.createInstance(fromIndex: index)
}

// Create by name
for name in viewModel.instanceNames {
let instanceByName = viewModel.createInstance(fromName: name)
}

绑定​

创建视图模型实例后,需要将其绑定到运行中的状态机或 Artboard。绑定之后,编辑器中连接到该视图模型的元素就会使用实例中的数据。

当前 Apple Runtime​

创建并保留你要读取、观察或修改的实例,然后在创建状态机时绑定它。请在 @MainActor 的 async 上下文中运行设置。

let artboard = try await file.createArtboard()
let viewModelInstance = try await file.createViewModelInstance(
.viewModelDefault(from: .artboardDefault(artboard))
)
let stateMachine = try await artboard.createStateMachine(binding: viewModelInstance)
let rive = try await Rive(file: file, artboard: artboard, stateMachine: stateMachine)

若要稍后替换主实例,请创建新实例并使用异步抛出的绑定 API:

let replacementInstance = try await file.createViewModelInstance(
.viewModelDefault(from: .artboardDefault(artboard))
)
try await stateMachine.bindViewModelInstances(main: replacementInstance)

绑定成功后,保留 replacementInstance 以便后续访问属性。

带显式画板和状态机的 Rive 初始化器会按已配置的组件使用它们,而不会重新绑定。请从同一文件创建画板,并从该画板创建状态机。使用来自同一文件和 worker 的兼容实例。

⚠️

绑定后的实例无法从状态机取回。使用新的初始化器时,已弃用的 Rive.viewModelInstance 属性会返回 nil。请自行保留引用以访问属性。参见迁移说明。

Legacy Apple Runtime​

let riveViewModel = RiveViewModel(...)
let artboard = riveViewModel.riveModel!.artboard,
let instance = riveViewModel.riveModel!.riveFile.defaultViewModel(for: artboard).createDefaultInstance()!

// Apply the instance to the state machine (preferred)
// Applying to a state machine will automatically bind to its artboard
riveViewModel.riveModel!.stateMachine.bind(instance)

// Alternatively, apply the instance to the artboard
artboard.bind(viewModelInstance: instance)

自动绑定​

自动绑定会尝试为当前 Artboard 或状态机找到合适的默认视图模型实例,并自动将其绑定。对于简单场景,这可以减少手动创建和绑定实例的代码。

当前 Apple Runtime​

创建状态机时,如果可用,会绑定编辑器中创作的主视图模型和全局默认实例。如果你不需要对这些实例进行编程访问,可以使用便捷初始化器:

let rive = try await Rive(file: file)

若要选择画板并使用其默认状态机:

let artboard = try await file.createArtboard("Artboard")
let rive = try await Rive(file: file, artboard: artboard)

自动创建的实例没有公开 getter。如果需要读取或更改它们的属性,请按绑定所示显式创建并保留实例。

Legacy Apple Runtime​

let riveViewModel = RiveViewModel(...)
riveViewModel.riveModel?.enableAutoBind { instance in
// Store a reference to `instance` to later access properties
// The instance may change as state machines and artboards change
}

// If you'd like to disable autoBind after enabling…
riveViewModel.riveModel!.disableAutoBind()

全局视图模型(Global View Models)​

在新的 Apple 运行时中,全局视图模型向编辑器中绑定到该全局的属性提供共享数据,例如主题。按全局视图模型名称创建实例并绑定它们:

let artboard = try await file.createArtboard()
let main = try await file.createViewModelInstance(
.viewModelDefault(from: .artboardDefault(artboard))
)
// 此示例要求文件中存在名为 "Theme" 的全局视图模型。
let theme = try await file.createViewModelInstance(
.viewModelDefault(from: .name("Theme"))
)
let stateMachine = try await artboard.createStateMachine(binding: main) {
("Theme", theme)
}
let rive = try await Rive(file: file, artboard: artboard, stateMachine: stateMachine)

要发现文件包含哪些全局视图模型,使用 try await file.getGlobalViewModelNames()。

在此示例中,如果你需要访问它们的属性,请将 main 和 theme 保存在应用的模型中。全局实例使用与主实例相同的属性和观察 API。要在来自同一文件的多个状态机之间共享一个实例,将该同一实例绑定到每个状态机。

你可以稍后只替换某个全局绑定:

let darkTheme = try await file.createViewModelInstance(
.name("Dark", from: .name("Theme"))
)
try await stateMachine.bindViewModelInstances {
("Theme", darkTheme)
}

globals builder 支持 if、if let 和 for 语句。例如,在 "Theme" 视图模型上创作的名为 "Light" 和 "Dark" 的实例之间进行选择:

let lightTheme = try await file.createViewModelInstance(
.name("Light", from: .name("Theme"))
)
let darkTheme = try await file.createViewModelInstance(
.name("Dark", from: .name("Theme"))
)
// colorScheme 是应用中当前的 SwiftUI ColorScheme 值。
try await stateMachine.bindViewModelInstances {
if colorScheme == .dark {
("Theme", darkTheme)
} else {
("Theme", lightTheme)
}
}

builder 在你调用绑定方法时运行。要在配色方案变化时切换主题,请用更新后的值和你保留的实例再次调用它。

你也可以传入 main:,在一次调用中同时更新主绑定和全局绑定。每次调用只设置所提供的绑定,并不会替换整套绑定。省略某个全局不会移除它或清空其实例——现有绑定保持不变。没有现有或新提供实例的槽位,会在可用时接收编辑器中创作的默认值。为 main 传入 nil 同样会保留其现有绑定,而不是清空它。

所有提供的全局名称会在应用任何绑定之前先校验。未知或非全局名称会抛出 StateMachineError.invalidGlobalViewModelName;同一次调用中的重复名称会抛出 StateMachineError.duplicateGlobalViewModelInstance。这些被拒绝的调用会保持现有绑定不变。元数据失败会抛出 StateMachineError.error,取消会抛出 StateMachineError.cancelled。名称校验不会检查实例类型或 worker 兼容性。

属性​

属性是视图模型中可绑定的数据字段。Rive 支持多种属性类型,包括字符串、数字、布尔值、颜色、枚举、触发器、图片、字体、Blob、列表和 Artboard。运行时通过视图模型实例访问这些属性。

列出属性​

你可以查询视图模型中定义的属性,以了解属性名称、类型和可用元数据。

当前 Apple Runtime​

let file: File = ...
let properties = try await file.getProperties(of: "ViewModel")
for property in properties {
print(property.type) // enum of string, number, boolean, etc
print(property.name) // The name of the property within the view model
print(property.metaData) // Additional metadata for the property, if available
}

Legacy Apple Runtime​

let riveViewModel = RiveViewModel(...)
let viewModel = riveViewModel.riveModel!.file.viewModelNamed(...)!
for property in viewModel.properties {
print(property.type) // String, number, boolean, etc
print(property.name) // The name of the property within the view model
}

读取和写入属性​

读取和写入属性是数据绑定最常见的操作。你可以通过属性路径访问视图模型实例上的属性,并根据属性类型读取或更新其值。

当前 Apple Runtime​

属性类型只是对属性路径和返回类型的轻量封装。

属性 API 在 ViewModelInstance 上提供。请从 main-actor 上下文中调用它们。读取 String、Number、Boolean、Color 或 Enum 属性使用 try await。设置属性会将变更入队并立即返回。

属性Swift 值支持的操作
StringPropertyString读、写、valueStream(of:)
NumberPropertyFloat读、写、valueStream(of:)
BoolPropertyBool读、写、valueStream(of:)
ColorPropertyColor读、写、valueStream(of:)
EnumPropertyString读、写、valueStream(of:)
TriggerProperty无存储值fire(trigger:)、stream(of:)
ImagePropertyImage?写;nil 清除
FontPropertyFont?写;nil 清除
BlobPropertyBlob?写;nil 清除
ArtboardPropertyArtboard?写;nil 清除
ViewModelInstancePropertyViewModelInstance读取并替换嵌套实例
ListProperty视图模型实例列表Count、get、append、insert、remove、swap

Image、Font、Blob 和 Artboard 属性不暴露值 getter 或值 stream。视图模型实例会保留赋给这些属性的值,直到它们被替换、用 nil 清除,或实例被释放。在赋值之前,请对任何需要保持存活的已解码图像、字体、blob 或已创建的画板保持强引用。作为全局资源注册的图像和字体也会由 worker 保留。

嵌套实例和列表项暴露各自的属性 API。

String​

let file: File = ...
let viewModelInstance = try await file.createViewModelInstance(...)

// String
let stringProperty = StringProperty(path: "path/to/string")
let stringValue = try await viewModelInstance.value(of: stringProperty)
viewModelInstance.setValue(of: stringProperty, to: "value")

Number​

let file: File = ...
let viewModelInstance = try await file.createViewModelInstance(...)

let numberProperty = NumberProperty(path: "path/to/number")
let numberValue = try await viewModelInstance.value(of: numberProperty)
viewModelInstance.setValue(of: numberProperty, to: 9001)

Bool​

let file: File = ...
let viewModelInstance = try await file.createViewModelInstance(...)

let boolProperty = BoolProperty(path: "path/to/bool")
let boolValue = try await viewModelInstance.value(of: boolProperty)
viewModelInstance.setValue(of: boolProperty, to: true)

Color​

let file: File = ...
let viewModelInstance = try await file.createViewModelInstance(...)

let colorProperty = ColorProperty(path: "path/to/color")
let colorValue = try await viewModelInstance.value(of: colorProperty)
viewModelInstance.setValue(of: colorProperty, to: Color(red: 255, green: 255, blue: 255, alpha: 255))

Enum​

let file: File = ...
let viewModelInstance = try await file.createViewModelInstance(...)

let enumProperty = EnumProperty(path: "path/to/enum")
let enumValue = try await viewModelInstance.value(of: enumProperty)
viewModelInstance.setValue(of: enumProperty, to: "value")

Trigger​

let file: File = ...
let viewModelInstance = try await file.createViewModelInstance(...)

let triggerProperty = TriggerProperty(path: "path/to/trigger")
viewModelInstance.fire(trigger: triggerProperty)

Legacy Apple Runtime​

let riveViewModel = RiveViewModel(...)

var viewModelInstance: RiveDataBindingViewModel.Instance!

// You can get the view model instance when enabling auto binding
riveViewModel.riveModel?.enableAutoBind { instance in
// Store a reference to instance
viewModelInstance = instance
}

// Alternatively, you can create a view model instance manually
viewModelInstance = riveViewModel.riveModel!.riveFile.viewModelNamed("...")!.createDefaultInstance()!

// Strings
let stringProperty = instance.stringProperty(fromPath: "...")!
// Updating its value
stringProperty.value = "Hello, Rive"
// Get its value
print(stringProperty.value)

// You can also set and get values without storing a strong reference
instance.stringProperty(fromPath: "...").value = "Hello again, Rive"

// Numbers
let numberProperty = instance.numberProperty(fromPath: "...")!
// Updating its value
numberProperty.value = 1337
// Get its value
print(numberProperty.value)

// You can also set and get values without storing a strong reference
instance.numberProperty(fromPath: "...").value = 1337

// Booleans
let booleanProperty = instance.booleanProperty(fromPath: "...")!
// Updating its value
booleanProperty.value = true
// Get its value
print(booleanProperty.value)

// You can also set and get values without storing a strong reference
instance.booleanProperty(fromPath: "...").value = true

// Colors
let colorProperty = instance.colorProperty(fromPath: "...")!
// Updating its value, which is a UIColor/NSColor, so all static helpers apply.
colorProperty.value = .red
// Get its value
print(colorProperty.value)

// You can also set and get values without storing a strong reference
instance.colorProperty(fromPath: "...").value = .red

// Enums
let enumProperty = instance.enumProperty(fromPath: "...")!
// Updating its value
enumProperty.value = "Foo"
// Get its value
print(enumProperty.value)
// Print all possible values
print(enumProperty.values)

// You can also set and get values without storing a strong reference
instance.enumProperty(fromPath: "...").value = "Foo"

// Trigger
let triggerProperty = instance.triggerProperty(fromPath: "...")!
// Fire the trigger
triggerProperty.trigger()

嵌套属性路径​

视图模型可以包含嵌套结构。你可以通过完整路径访问嵌套属性。

当前 Apple Runtime​

使用 / 分隔的路径直接访问嵌套属性,或通过 ViewModelInstanceProperty 获取嵌套实例:

let title = StringProperty(path: "header/title")
viewModelInstance.setValue(of: title, to: "Welcome")

let headerProperty = ViewModelInstanceProperty(path: "header")
let header = viewModelInstance.value(of: headerProperty)
header.setValue(of: StringProperty(path: "title"), to: "Welcome")

let replacement = try await file.createViewModelInstance(
.viewModelDefault(from: .name("Header"))
)
viewModelInstance.setValue(of: headerProperty, to: replacement)

请用与嵌套属性兼容的视图模型创建替换实例,并使用同一个 worker。

Legacy Apple Runtime​

let riveViewModel = RiveViewModel(...)

var viewModelInstance: RiveDataBindingViewModel.Instance!

// You can get the view model instance when enabling auto binding
riveViewModel.riveModel?.enableAutoBind { instance in
// Store a reference to instance
viewModelInstance = instance
}

// Alternatively, you can create a view model instance manually
viewModelInstance = riveViewModel.riveModel!.riveFile.viewModelNamed("...")!.createDefaultInstance()!

let nestedNumberByChain = instance
.viewModelInstanceProperty(fromPath: "Nested View Model")
.viewModelInstanceProperty(fromPath: "Another Nested View Model")
.numberProperty(fromPath: "Number")

let nestedNumberByPath = instance.numberProperty(fromPath: "Nested View Model/Another Nested View Model/Number")

可观察性​

你可以监听属性值的变化,并在值变化或 trigger 被触发时响应。这适合用于将 Rive 动画状态同步回应用逻辑。

当前 Apple Runtime​

RiveController 会在绑定的主实例和全局实例(包括嵌套实例和列表子实例)发生变化后自动重绘。你不需要订阅已弃用的 dirtyStream() API 来请求重绘。使用属性 stream 来在应用中响应这些值。

String、Number、Boolean、Color 和 Enum 属性通过 Swift Concurrency 的 async throwing stream 暴露变化。在 ViewModelInstance 对象上调用 valueStream(of:)。

let file: File = ...
let viewModelInstance = try await file.createViewModelInstance(...)
let stringProperty = StringProperty(path: "path/to/string")
let valueStream = viewModelInstance.valueStream(of: stringProperty)
do {
for try await value in valueStream {
print(value)
}
} catch let error as ViewModelInstanceError {
// The thrown error should always be a ViewModelInstanceError type
print(error)
} catch {
print(error)
}

对于 trigger,你可以在 ViewModelInstance 对象上调用 stream(of:) 方法来监听。它会返回一个 Void 值的 stream,你可以忽略这些值。

let file: File = ...
let viewModelInstance = try await file.createViewModelInstance(...)
let triggerProperty = TriggerProperty(path: "path/to/trigger")
let triggerStream = viewModelInstance.stream(of: triggerProperty)
do {
for try await _ in triggerStream {
print("Trigger fired!")
}
} catch let error as ViewModelInstanceError {
// The thrown error should always be a ViewModelInstanceError type
print(error)
} catch {
print(error)
}

Legacy Apple Runtime​

let riveViewModel = RiveViewModel(...)

var viewModelInstance: RiveDataBindingViewModel.Instance!

// You can get the view model instance when enabling auto binding
riveViewModel.riveModel?.enableAutoBind { instance in
// Store a reference to instance
viewModelInstance = instance
}

// Alternatively, you can create a view model instance manually
viewModelInstance = riveViewModel.riveModel!.riveFile.viewModelNamed("...")!.createDefaultInstance()!

// Get the string property
let stringProperty = instance.stringProperty(fromPath: "...")!

// Add a listener
let listener = stringProperty.addListener { newValue in
print(newValue)
}

// Remove a listener, where listener is the return value of addListener
stringProperty.removeListener(listener)

// Trigger properties can also be listened to for when they are triggered
instance.triggerProperty(fromPath: "...")!.addListener {
print("Triggered!")
}

图片​

图片属性允许你在运行时替换 Rive 文件中绑定的图片资源。

当前 Apple Runtime​

要设置图片,首先需要通过 Worker 解码图片。这个 Worker 必须与初始化 File 时使用的 Worker 相同;你要设置的 Image 属性也来自该 File 创建的视图模型实例。

let worker = try await Worker()
let file = try await File(source: ..., worker: worker)
let viewModelInstance = try await file.createViewModelInstance(...)
let imageProperty = ImageProperty(path: "path/to/image")
let imageData: Data = ...
let image = try await worker.decodeImage(from: imageData)
viewModelInstance.setValue(of: imageProperty, to: image)
// 需要时清除图片。
viewModelInstance.setValue(of: imageProperty, to: nil)

Legacy Apple Runtime​

let riveViewModel = RiveViewModel(...)

var viewModelInstance: RiveDataBindingViewModel.Instance!

// You can get the view model instance when enabling auto binding
riveViewModel.riveModel?.enableAutoBind { instance in
// Store a reference to instance
viewModelInstance = instance
}

// Alternatively, you can create a view model instance manually
viewModelInstance = riveViewModel.riveModel!.riveFile.viewModelNamed("...")!.createDefaultInstance()!

// Create a RiveRenderImage from data
let data = Data(...)
var image = RiveRenderImage(data: data)! // This can return nil if the data is not a valid image

// Or, create a RiveRenderImage from a UIImage
image = RiveRenderImage(image: UIImage(named: "my_image")!, format: .png)! // This can return nil if the image is not a valid jpg or png image

let imageProperty = viewModelInstance.imageProperty(fromPath: "image")!

// Once you have your data binding view model instance, you can set the Image property value
imageProperty.setValue(image)

// You can also pass nil to clear the image
imageProperty.setValue(nil)

字体(Fonts)​

在 Apple 并发 API 中,使用与实例所属文件相同的 Worker 解码字体,然后将其赋给 FontProperty。

let fontData: Data = ...
let font = try await worker.decodeFont(from: fontData)
let fontProperty = FontProperty(path: "headingFont")
viewModelInstance.setValue(of: fontProperty, to: font)

// 需要时清除字体。
viewModelInstance.setValue(of: fontProperty, to: nil)

decodeFont(from:) 在 UIKit 平台上也接受 UIFont,在 macOS 上接受 NSFont。解码和全局注册选项见加载资源。

Blobs​

Blob 保存任意二进制数据。使用与实例所属文件相同的 worker 解码它,然后赋给 Blob 属性:

let data = Data([0x01, 0x02, 0x03])
let blob = try await worker.decodeBlob(from: data)
let blobProperty = BlobProperty(path: "payload")
viewModelInstance.setValue(of: blobProperty, to: blob)

// 需要时清除 blob。
viewModelInstance.setValue(of: blobProperty, to: nil)

列表​

列表属性可以包含多个视图模型实例。你可以追加、插入、交换、删除和读取列表中的实例。

当前 Apple Runtime​

let file: File = ...
let viewModelInstance = try await file.createViewModelInstance(...)
let listProperty = ListProperty(path: "path/to/list")

let size = try await viewModelInstance.size(of: listProperty)

// Result: [newInstance]
let newInstance = try await file.createViewModelInstance(...)
viewModelInstance.appendInstance(newInstance, to: listProperty)

// Result: [insertedInstance, newInstance]
let insertedInstance = try await file.createViewModelInstance(...)
viewModelInstance.insertInstance(insertedInstance, to: listProperty, at: 0)

// Result: [newInstance, insertedInstance]
viewModelInstance.swapInstance(atIndex: 0, withIndex: 1, in: listProperty)

// Result: [newInstance]
viewModelInstance.removeInstance(at: 1, from: listProperty)

// Result: newInstance
let _ = viewModelInstance.value(of: listProperty, at: 0)

// Result: []
viewModelInstance.removeInstance(newInstance, from: listProperty)

// Result: 0
let finalSize = try await viewModelInstance.size(of: listProperty)

Legacy Apple Runtime​

let listProperty = viewModelInstance.listProperty(fromPath: "list")!

// Create a new view model instance and add it to the end of the list
let firstInstance = viewModel.createInstanceByName("First Instance")!
listProperty.add(firstInstance)

// Create a new view model instance and add it to the beginning of the list
let secondInstance = myViewModel.createInstanceByName("Second Instance")!
listProperty.add(secondInstance, atIndex: 0)

// Swap the first and second instances
listProperty.swapInstance(atIndex: 0, withInstanceAtIndex: 1)

// Remove both instances
listProperty.removeInstance(secondInstance)
listProperty.removeInstance(atIndex: 0)

// Get and print the size of the list
print(listProperty.size) // Prints 0

Artboard​

Artboard 属性允许你在运行时替换绑定到某个属性的 Artboard。

当前 Apple Runtime​

let file: File = ...
let viewModelInstance = try await file.createViewModelInstance(...)
let artboardProperty = ArtboardProperty(path: "path/to/artboard")
let artboard = try await file.createArtboard(...)
viewModelInstance.setValue(of: artboardProperty, to: artboard)
// 需要时清除画板。
viewModelInstance.setValue(of: artboardProperty, to: nil)

Legacy Apple Runtime​

使用 RiveDataBindingViewModel.Instance 对象上的 artboardProperty 方法获取 Artboard 属性。

然后使用 Artboard 属性对象上的 setValue 方法设置新的 Artboard 值。

setValue 接收一个 RiveBindableArtboard 对象,它是 Artboard 的包装对象,可用于设置 Artboard 属性值。

你可以通过 RiveFile 对象上的 bindableArtboard 方法获取 RiveBindableArtboard 对象。

let artboardProperty = instance.artboardProperty(fromPath: "Artboard")!

let components = RiveFile(...)
let bindableArtboard = components.bindableArtboard(at: 0)!
let bindableArtboard2 = components.bindableArtboard(withName: "...")!

artboardProperty.setValue(bindableArtboard)

枚举​

枚举属性用于在一组预定义字符串值之间切换。你可以读取当前枚举值、设置新的枚举值,并在 Legacy API 中读取可用枚举值列表。设置枚举值时,需要确保传入的字符串与 Rive 文件中定义的枚举选项匹配。

示例​

请参阅示例应用中的 Data Binding view 获取演示。

看完还有疑问?进群交流下!
与众多 Rive 创作者、开发者一起交流探讨与答疑解惑。
加入交流群