在 SwiftUI 中使用 combine 更新 Lottie Animation 时,它无法在正确的时间更新并给出意外结果

问题描述

所以我有这个 viewmodel 与一个计时器一起使用,我希望这个 viewmodel 用新动画更新 LottieView 和文件名。当计时器倒计时时,我希望它发布和发送特定的字符串,这些字符串将是 json Lottie 文件名。当我的 ContentView 收到这些文件名时,我希望它动态更新 LottieViews 动画。因此,我在 ContentView 中创建了一个名为 name 的 @State 变量,并使其等于传入的接收值。 然而,让我感到困惑的是,在 10 秒标记处从 viewmodels 计时器发布和发送的文件名是假设在 LottieView(filename: name) 中接收和使用。然而,当我启动应用程序时,这个 LottieView 会立即运行这个文件。为何如此?文件名在整个应用程序中唯一存在的地方是计时器达到 10 秒时,甚至在调用 LottieView(name) 时它都不应该存在。它还会忽略本应在 19 秒标记处运行的先前文件名。如果我要一起忽略 LottieView(name) 并运行文本视图,那么在这种情况下,Text(name),当我运行应用程序时,当计时器达到 10 时,Text 会正确更改。 那么 LottieView(name) 怎么运行像这样?我验证了这些文件也与它们的动画正确匹配。期待您的回复。谢谢!

import SwiftUI
import Combine

class viewmodel: ObservableObject {
    
    var anyCancellable: AnyCancellable?
    let publish = PassthroughSubject<String,Never>()
    
    private var timer: Timer?
    private var scheduleTime = 20
    
    init() {
        fire()
        anyCancellable = publish.sink { str in
            print("Value that is being passed over: \(str)")
        }

    }
    
    func fire() {
        print("Fire timer")
        timer = Timer.scheduledTimer(withTimeInterval: 1.0,repeats: true) { timer in
            
            
            self.scheduleTime -= 1
            print(self.scheduleTime)
            if self.scheduleTime == 19 {
                self.publish.send("13865-sign-for-error-flat-style")
            }
            if self.scheduleTime == 10 {
               self.publish.send("4174-unlock-to-premium")
                timer.invalidate()
            }
        }
    }
}


import SwiftUI
import Combine

struct ContentView: View {
    
    @Observedobject var vm = viewmodel()
    @State var name: String = ""
    var body: some View {
      
        vstack {
            LottieView(filename: $name)
            Text(name)
        }
            .onReceive(vm.publish,perform: { value in
                print("passed over : \(value)")
                name = value
                print(name)
            })
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


import SwiftUI
import Lottie

struct LottieView: UIViewRepresentable {
    
  typealias UIViewType = UIView
  @Binding var filename: String
  
  func makeUIView(context: UIViewRepresentableContext<LottieView>) -> UIView {
    let view = UIView(frame: .zero)
    
    let animationView = AnimationView()
    let animation = Animation.named(filename)
    animationView.animation = animation
    animationView.contentMode = .scaleAspectFit
    animationView.play()
    
    animationView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(animationView)
    
    NSLayoutConstraint.activate([
      animationView.widthAnchor.constraint(equalTo: view.widthAnchor),animationView.heightAnchor.constraint(equalTo: view.heightAnchor),animationView.centerXAnchor.constraint(equalTo: view.centerXAnchor),animationView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
    ])
    
    return view
  }
  
  func updateUIView(_ uiView: UIView,context: UIViewRepresentableContext<LottieView>) {
  }
    
}

Console output when the code runs

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)