簡體   English   中英

如何在應用程序啟動之前播放動畫?

[英]how can I play animation when before application start?

我想為加載屏幕添加動畫。 我使用了網絡服務。 當我獲取數據時,如何在等待獲取數據時添加動畫。(我使用 lottie pod)這是我的代碼:

        animationViewLoading!.frame = self.view.bounds
        animationViewLoading!.backgroundColor = UIColor.darkGray
        animationViewLoading!.contentMode = .scaleAspectFit
        animationViewLoading!.loopMode = .loop
        animationViewLoading!.animationSpeed = 1.0
        self.view.addSubview(animationViewLoading!)
        animationViewLoading!.play()

此代碼用於從網絡服務獲取數據

func getDataformWebService(){

let semaphore = DispatchSemaphore (value: 0)
var request = URLRequest(url: URL(string: "http://moodytest-env.eba-mmzgp9iv.eu-central-1.elasticbeanstalk.com/api/categories")!,timeoutInterval: Double.infinity)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("Bearer \(APP_TOKEN)", forHTTPHeaderField: "Authorization")

request.httpMethod = "GET"

let task = URLSession.shared.dataTask(with: request) { data, response, error in
  guard let data = data else {
    print(String(describing: error))
    return
  }
    do{
        // splash screen creat  and property
       
        
        timersplash = Timer.scheduledTimer(timeInterval: 1, target: ViewController.self, selector: #selector(ViewController.timerfuncloading), userInfo: nil, repeats: true)
        let categoriesDetails : CategoriesDetails = try JSONDecoder().decode(CategoriesDetails.self, from: data)
        category.append(contentsOf: categoriesDetails.categories)
   
        
    }catch{
        print(error.localizedDescription)
    }
  semaphore.signal()
}

task.resume()
semaphore.wait()

}

您使用的是什么網絡服務? 如果您詢問 api 調用,一種方法是使用 dispatchGroup。

例如:

func getData(){

   let dispatchGroup = DispatchGroup()

   dispatchGroup.enter()
   FetchData().fetchData(url: url) { (data, error) in
           print(data) //load or print data
           dispatchGroup.leave()
   }


    dispatchGroup.notify(queue: .main) {
         animationViewLoading.stop()   //Stop the animation after data has 
                                         finished printing or loading
    }

}

如果您需要進一步的幫助,請告訴我!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM