ios - 呈现模态视图 Controller 时暂停动画

标签 ios swift lottie

我有两个动画在我的主屏幕 View Controller 上播放。它们都是来自 LottieFiles 的动画。一个是 alpha 颜色变化的背景,另一个是不同颜色的无限循环。当选择当前上下文上的新模态视图时,我想暂停两个动画,并在模态视图关闭后再次启动它们。

我尝试调用animationView.pause方法,但它们仍然继续在后台播放。我想暂停它们以提高能源效率。

class HomeScreen: UIViewController{
 let animationView = AnimationView()
 let animationTwo = AnimationView()

 func showSupport() {
    
    let modalViewController = SupportViewController()
    modalViewController.modalPresentationStyle = .overCurrentContext
    present(modalViewController, animated: true, completion: nil)
}
 func showSInfo() {
    
    let modalViewController = InfoViewController()
    modalViewController.modalPresentationStyle = .overCurrentContext
    present(modalViewController, animated: true, completion: nil)
}
override func viewDidAppear(_ animated: Bool) {
    
    super.viewDidAppear(animated)
    
    
    animationView.play(fromProgress: 0,
                       toProgress: 1,
                       loopMode: LottieLoopMode.loop,
                       completion: { (finished) in
                        if finished {
                            print("Animation Complete")
                        } else {
                            print("Animation cancelled")
                        }
    })
    
    
    animationTwo.play(fromProgress: 0,
                       toProgress: 1,
                       loopMode: LottieLoopMode.loop,
                       completion: { (finished) in
                        if finished {
                            print("Animation Complete")
                        } else {
                            print("Animation cancelled")
                        }
    })
    
}
 override func viewDidLoad() {
   let animation = Animation.named("bg12")
    animationView.animation = animation
    animationView.contentMode = .scaleAspectFill
    view.addSubview(animationView)

    let animationViewTwo = Animation.named("infi2")
    animationTwo.animation = animationViewTwo
    animationTwo.contentMode = .scaleAspectFit
    animationTwo.alpha = 0.7
    view.addSubview(animationTwo)
 }
    }


 class SupportViewController: UIViewController {
     let homeVC = HomeScreen()
    
  override func viewDidLoad() {
    homeVC.animationView.pause()
    homeVC.animationTwo.pause()
    super.viewDidload()
}
}

最佳答案

你不能将暂停放到当前的完成处理程序中,例如:

present(modalViewController, animated: true) {
    self.animationView.pause()
}

关于ios - 呈现模态视图 Controller 时暂停动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58516823/

相关文章:

ios - UITableView 在 iAd 下滚动

iOS 已弃用的 dataWithContentsOfMappedFile 替换

json - SwiftyJSON 问题 Swift 2

ios - “_OBJC_CLASS_$_” ,引用自 : error in xcode 4. 3.2

html - 在 iOS 全屏 Web 应用程序中使用链接

android - 为什么我的 Lottie 动画加载这么慢?

android - 如果不使用 Card 覆盖,Compose Lottie 组件将无法正常工作

flutter - Flutter 中 After Effects 文件的使用

android - 调试需要服务器 URL 的应用程序

ios - 如何使用 MKMapView 绘制或制作自定义路线