【问题标题】:Lottie: Can't get Animation with nameLottie:无法获得带有名称的动画
【发布时间】:2018-03-04 21:49:34
【问题描述】:

我正在使用 Lottie 2.1.3、XCode 9 和 IOS 11。

当我试图获得这样的动画时

var loadingView = LOTAnimationView(name: "preloader")

我遇到了这个错误:+[LOTComposition animationNamed:inBundle:]: Animation Not Found

但是获得如下动画效果很好

var loadingView = LOTAnimationView(filePath: "/Users/username/Git/iOS/Swift/LottieTest/LottieTest/preloader.json")

这是我正在使用的 preloader.json https://www.lottiefiles.com/storage/datafiles/Hhw0wgYmETDTkxW/data.json

我在这里做错了什么?

【问题讨论】:

    标签: ios animation swift3 xcode9 lottie


    【解决方案1】:

    很有意思,但是你不想使用其他的init方法,比如使用json文件吗?

    我查看了 Lottie 的文档,但似乎找不到这个初始化函数 LOTAnimationView(name: "name") 背后的解释。正如我们在 Lottie 的示例中看到的那样,LottieLogo.json 文件的数据与您在问题中提供的 json 文件以及我自己的项目中的 json 文件相比具有不同的数据。 p>

    尽管如此,只需将您的 json 文件添加到您的项目中并读取它并使用 Lottie 的这个 init 函数 --> LOTAnimationView(json: jsonObject)

    我在名为GPKit https://github.com/glennposadas/gpkit-ios 的小项目中创建了一个读取json文件的函数:D

    public class GPJSONReader {
    
        /** Get the whole JSON object from a file
         *  @returns an optional dictionary [String: Any]?
         */
    
        public class func readJson(fileName: String, withBlock completion: ([String : Any]?) -> Void) {
            do {
                if let file = Bundle.main.url(forResource: fileName, withExtension: "json") {
                    let data = try Data(contentsOf: file)
                    let json = try JSONSerialization.jsonObject(with: data, options: [])
                    if let object = json as? [String : Any] {
                        GPLog(classSender: self, log: "JSON found as dictionary")
                        completion(object)
                    } else {
                        GPLog(classSender: self, log: "JSON is invalid")
                        completion(nil)
                    }
                } else {
                    GPLog(classSender: self, log: "No file found!")
                    completion(nil)
                }
            } catch {
                GPLog(classSender: self, log: error.localizedDescription)
                completion(nil)
            }
        }
    }
    

    然后像这样对 Lottie 使用上面的函数:

    // Animate here...
    GPJSONReader.readJson(fileName: "connecting", withBlock: { (jsonObject) in
        if let jsonObject = jsonObject {
            self.animationView = LOTAnimationView(json: jsonObject)
            self.animationView.loopAnimation = true
            self.animationView.play()
        }
    })
    

    【讨论】:

    • 感谢您的回答。我想我必须像你说的那样使用其他初始化方法。我相信lottie有一个错误,我打开了一个关于它的问题。 github.com/airbnb/lottie-ios/issues/430
    【解决方案2】:

    我解决了我的问题。显然那是我的错。 为了使用LOTAnimationView(name: "name") init 方法,您必须执行以下操作...

    在您的 XCode 项目中,单击您的 preLoader.json 文件,然后在检查器(Xcode 的右侧面板)中单击 Target MemberShip 中带有项目名称的复选框

    【讨论】:

      【解决方案3】:

      我正在使用

        let animationView = LOTAnimationView(name: "servishero_loading")
                  animationView.frame = CGRect(x: 0, y: 0, width: 300, height: 400)
                  animationView.center = self.view.center
                  animationView.contentMode = .scaleAspectFill
      
                  view.addSubview(animationView)
      
                  animationView.play()
      

      我解决了我的问题,只需单击 servishero_loading.json 文件并在 File Inspector ->Target Membership -> 选中项目目标名称的复选框。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-04
        • 1970-01-01
        • 2021-11-04
        • 2020-11-22
        相关资源
        最近更新 更多