非常有趣,但你不想使用其他初始化方法,如使用json文件?
我检查了洛蒂的文档,我似乎无法找到这个初始化函数背后的解释LOTAnimationView(name: "name")
。正如我们在Lottie的例子中看到的,LottieLogo.json文件与您在问题中提到的json文件以及我的项目中我自己的json文件相比,具有不同的数据。
尽管如此,只要将你的JSON文件到您的项目和阅读并使用洛蒂的这个初始化函数 - >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)
}
}
}
然后使用功能上面洛蒂,像这样:
// 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()
}
})
谢谢您的回答。我想我必须使用其他初始化方法,像你说的。我相信loottie有一个bug,我开了一个关于它的问题。 https://github.com/airbnb/lottie-ios/issues/430 –