ios - 在应用程序启动之前如何播放动画?
问题描述
我想为加载屏幕添加动画。我使用了网络服务。当我获取数据时,如何在等待获取数据时添加动画。(我使用 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()
}
解决方案
您使用的是什么网络服务?如果您询问 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
}
}
如果您需要进一步的帮助,请告诉我!
推荐阅读
- github - 将我的第一个项目从我的 Git 推送到我的在线 GitHub 存储库
- java - 放心如何忽略 api 响应中的参数并将其与 json 进行比较
- javascript - React JS - 自动添加行
- swift - Cocoa:如何取消归档具有归档属性的 NSMutableAttributedString?
- android - 如何将变量转换为常量
- jestjs - 为什么 .mock.instances 打印这个?
- google-play - 在 google play 的主搜索中找不到应用程序
- bash - 如有必要,使用 sed 添加子网掩码格式化 IP 地址列表
- c# - 在 Windows 窗体项目的 Main 中初始化 Class Form 实例的原因是什么?
- android - 如何将基于 Cradle 的 Android Studio 项目导入 Visual Studio 2017 或 2019?