ios - Lottie:无法获取带名称的动画

标签 ios animation swift3 xcode9 lottie

我正在使用 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

我做错了什么?

最佳答案

很有趣,但是你不想使用其他初始化方法,例如使用 json 文件吗?

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

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

我在名为 GPKit 的小项目中创建了一个读取 json 文件的函数 https://github.com/glennposadas/gpkit-ios :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()
    }
})

关于ios - Lottie:无法获取带名称的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46379562/

相关文章:

ios - 隐藏和取消隐藏 SKSprite 节点

ios - 滚动到特定标题 View

ios - 从 JSON 从 NSDictionary 中获取键值对

CSS 动画关键帧中断媒体查询

swift - 如何回放录制的视频?

ios - Swift 4 UIButton 附加在 View 上

ios - Swift 计数停止在错误的数字

css - 如何将自动播放(自动移动/自动滑动)添加到 csslider?

javascript - 如何根据图像中显示的百分比进行星级评定?

ios - UIViewController 中的 Collection View 可选展开崩溃