Jas*_*Dev 7 xcode swift swift3 lottie
我正在尝试使用 Xcode 11.2 制作一个简单的游戏,其中包含一个动画循环背景和一个视图,该视图显示和更改项目中多个 Lottie JSON 文件的各种动画。
当我单击“下一步”按钮更改视图动画时,循环的背景视图会卡住一秒钟,直到加载视图内的下一个动画,应用程序中的所有内容都会像背景动画一样同时冻结。
CPU 使用率从 30% 到 63% 不等。
我不想让问题复杂化,所以我只是展示我使用 Lottie 的方式。
@IBAction func SubmitButtonAction(_ sender: UIButton) {
showNextQuestion()
}
func showNextQuestion()->(){
myTimer.invalidate()
startCountdown(fromTime: 15)
Manager.generateQuestion()
lblLevel.text = String(Manager.questionNumber) + "/" + String(DataModel.Questions.count)
nIndex = 0
let animation = AnimationView(name: Manager.currentImage)
animation.loopMode = .loop
animation.play()
animation.backgroundColor = UIColor.clear
animation.frame = self.AnimView.bounds
animation.backgroundBehavior = .pauseAndRestore
if AnimView.subviews.isEmpty{
self.AnimView.addSubview(animation)
}
else {
for one in imgImageView.subviews{
one.removeFromSuperview()
}
self.AnimView.addSubview(animation)
}
AnswerCollectionView.reloadData()
RandomCollectionView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)
我对有关该问题与什么相关的建议感兴趣 - 它是否与线程问题有关?
为了响应 Jaseel.Dev,我创建了一个返回 LottieView 的函数,其中包含以下内容:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
withAnimation(.spring()) {
LottieView(name: animation)
}
}
Run Code Online (Sandbox Code Playgroud)