非常有趣,但你不想使用其他初始化方法,如使用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 –