2017-09-23 26 views
0

获得动画我使用的是洛蒂2.1.3时,Xcode 9和IOS 11洛蒂:不能与名

当我试图让动画这样

var loadingView = LOTAnimationView(name: "preloader") 

我米服用此错误:+ [LOTComposition animationNamed:inBundle:]:动画未找到

但是,让动画像下面工作正常

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

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

我在做什么错在这里?

回答

0

我解决了我问题。这似乎是我的错。 为了使用LOTAnimationView(name: "name") init方法,你必须做以下...

在您的XCode项目,点击您preLoader.json文件,然后在检查器(Xcode中的右图)单击带有复选框您目标会员中的项目名称

1

非常有趣,但你不想使用其他初始化方法,如使用json文件?

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

尽管如此,只要将你的JSON文件到您的项目和阅读并使用洛蒂的这个初始化函数 - >LOTAnimationView(json: jsonObject)

我做了一个函数在GPKithttps://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() 
    } 
}) 
+0

谢谢您的回答。我想我必须使用其他初始化方法,像你说的。我相信loottie有一个bug,我开了一个关于它的问题。 https://github.com/airbnb/lottie-ios/issues/430 –