2017-09-23 43 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 –