很有意思,但是你不想使用其他的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()
}
})