Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在应用程序启动之前,如何播放动画?_Ios_Swift_Mobile - Fatal编程技术网

Ios 在应用程序启动之前,如何播放动画?

Ios 在应用程序启动之前,如何播放动画?,ios,swift,mobile,Ios,Swift,Mobile,我想为加载屏幕添加动画。我使用了web服务。当我获取数据时,如何在等待获取数据时添加动画。(我使用lottie pod) 这是我的密码: animationViewLoading!.frame = self.view.bounds animationViewLoading!.backgroundColor = UIColor.darkGray animationViewLoading!.contentMode = .scaleAspectFit

我想为加载屏幕添加动画。我使用了web服务。当我获取数据时,如何在等待获取数据时添加动画。(我使用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()
此代码用于从web服务获取数据

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()

}您使用的是什么web服务?如果您询问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
    }

}

如果你需要进一步的帮助,请告诉我

你说的申请开始之前是什么意思?在开始第一个活动之前对不起,我还是不知道understand@AlexH我希望在手机启动时首先运行动画。动画运行时,我希望数据来自web服务。我希望动画在来自web服务的数据完成时停止。调度组可能不必要地复杂;为什么不在闭包中停止动画?我试过了,但出现了错误:com.apple.scenekit.scnview-renderer(20):“在从主线程访问布局引擎后,不能从后台线程执行对布局引擎的修改。”您尝试在闭包中停止动画,或者尝试了我所说的内容,但出现了错误?如果我说的不起作用,那么您可以尝试执行DispatchQueue.main.async{…}。任何与UIView或UIKit有关的操作都必须在主线程中进行