【问题标题】:SWIFT UI LOTTIE : Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)SWIFT UI LOTTIE:线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)
【发布时间】:2019-11-07 22:45:42
【问题描述】:

我正在尝试使用 Lottie 和 SwiftUI 实现一个动画视图。

这是我的代码:

import SwiftUI
import Lottie

struct ContentView: View {
var body: some View {
    
    AnimationsView()
    }
}

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

func makeUIView(context: UIViewRepresentableContext<AnimationsView>) -> 
AnimationView {
   let aniView = AnimationView()
    let animation = Animation.named("Switch", subdirectory: "TestAnimations")
    aniView.animation = animation
    aniView.play()
    return aniView
    }
func updateUIView(_ uiView: AnimationView, context: 
UIViewRepresentableContext<AnimationsView>) {

    }
}

我已将 Lottie 的最新版本添加为 Swift 包依赖项。 SwiftUI 中的预览向我展示了动画,在这种状态下一切正常。我没有使用 Storyboard,它应该在里面打开 View 和 lottie 动画。

当我运行应用程序时,应用程序崩溃并且我收到以下消息代码:线程 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

据我了解,有些东西没有初始化,返回值为 null ...我正在尝试做与本教程相同的事情:https://www.youtube.com/watch?v=iuEqGyBYaE4

怎么了?

【问题讨论】:

    标签: swift swiftui lottie


    【解决方案1】:

    这是一个已知问题。你可以看到herehere

    有一种解决方法,方法是在应用的目标中将死代码剥离设置为 no

    DEAD_CODE_STRIPPING = NO
    

    同样,您可能还需要更改从捆绑包中访问 lottie 文件的方式。

    struct AnimationsView: UIViewRepresentable {
    
        func makeUIView(context: UIViewRepresentableContext<AnimationsView>) -> AnimationView {
            let aniView = AnimationView()
            // its always a good idea to check that the file exists and throw an error if it doesn't. 
            guard let animation = Animation.named("Switch", bundle: .main) else {
                fatalError("Couldn't load animation")
            }
            aniView.animation = animation
            aniView.loopMode = .repeat(10.0)
            return aniView
        }
    
        func updateUIView(_ uiView: AnimationsView.UIViewType, context: UIViewRepresentableContext<AnimationsView>) {
    
        }
    
    }
    

    【讨论】:

    • 只需要再问一个问题:自上一个版本以来,是否仍然可以在情节提要中包含lottie?我正在尝试这样做,但我在 ViewController 上遇到了一些问题,说“找不到可设计的”。你知道吗?谢谢!!
    • 很遗憾我不知道。
    猜你喜欢
    • 2017-04-20
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多