🪛 In Progress: Data Binding swiftui usage or bugs
I use a rive file including data binding in my swiftui project,but I cant refresh rive ui until I click rive view.here is my code :
import SwiftUI
import RiveRuntime
struct RiveDataBindingView: View {
@State var riveModel = RiveBindingViewModel()
var body: some View {
VStack{
riveModel.view().background(.black)
Button{
riveModel.changeScore()
}label: {
Text("refresh")
}
}
}
}
class RiveBindingViewModel: RiveViewModel {
private var instance : RiveDataBindingViewModel.Instance?
@Published var current : Float = 0.0
init(){
print("RiveBindingViewModel init")
super.init(fileName: "test-1",stateMachineName: "State Machine 1")
let viewModel = self.riveModel!.riveFile.viewModelNamed("Loader")!
for property in viewModel.properties {
print(property.type) // String, number, boolean, etc
print(property.name) // The name of the property within the view model
}
instance = viewModel.createDefaultInstance()
if let instance{ riveModel?.stateMachine!.bind(viewModelInstance: instance)
let maxProperty = instance.numberProperty(fromPath: "max")!
maxProperty.value = 50
let currentProperty = instance.numberProperty(fromPath: "current")!
let listener = currentProperty.addListener { newValue in
print(newValue)
}
}
}
func changeScore(){
if let instance{
print("instance change score")
let currentProperty = instance.numberProperty(fromPath: "current")!
currentProperty.value = currentProperty.value + 10
}
}
}
#Preview {
RiveDataBindingView()
}
and this is my rive file : https://rive.app/marketplace/17811-33444-data-binding-example-tutorial-in-file-loaders/