ios - 如何仅播放并自动反转 Lottie 动画一次?

标签 ios swift lottie

我目前正在尝试使用 Lottie 框​​架向我的应用程序添加动画。我的代码如下:

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .lightGray
        let starAnimationView = AnimationView()
        // The JSON file
        let starAnimation = Animation.named("qz-checklist-eve")
        starAnimationView.animation = starAnimation
        starAnimationView.frame = CGRect(x: 0, y: 0, width: 400, height: 400)
        starAnimationView.center = self.view.center
        starAnimationView.contentMode = .scaleAspectFill
        view.addSubview(starAnimationView)
        starAnimationView.loopMode = .autoReverse
        starAnimationView.play()
    }

这有效,但是动画不断循环。我似乎找不到如何将其设置为单个动画。我检查了 Lottie 文档,它说:

var AnimationView.loopMode: LottieLoopMode { 获取设置}

Sets the loop behavior for play calls. Defaults to playOnce Options: : playOnce: Animation is played once then stops. : loop: Animation will loop from end to beginning until stopped. : autoReverse: Animation will play forward, then backwards and loop until stopped. : repeat(amount): Animation will loop from end to beginning up to amount of times. : repeatBackwards(amount): Animation will play forward, then backwards a amount of times. #

没有.repeatBackwards(amount)选项。有谁知道如何让它发挥作用吗?

最佳答案

以下是它对我的工作方式:

    //play animation in full reverse
    animationView.play(fromFrame: lastFrame, toFrame: 1.0, loopMode: .playOnce) { finished in ... }

要获取到最后一帧,需要正常运行,运行完毕后捕获:

    animationView.loopMode = .playOnce

    animationView.play { finished in
            
        if finished {
            
            let lastFrame = animationView.currentFrame    
            print("Last frame is: \(animationView.currentFrame)")

        }

    }

关于ios - 如何仅播放并自动反转 Lottie 动画一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56969272/

相关文章:

ios - 具有恒定大小和动态字体大小的 UILabel 取决于文本

android - 使用 Nativescript 的 BottomNavigationBar 不起作用

swift - 使用 Swift 在一个函数中多次更新 Textview 文本

swift - 无法将纹理图像添加到 ARKit 中的立方体

javascript - 我如何使用 javascript 来实现这样用户只能在 Lottie 动画完全播放后触发悬停事件?

java - 如何将 Lottie 动画从 Kotlin 解释和翻译成 Java?

ios - 动画 TabBar 颜色变化

ios - 使用UITableViewController有什么好处?

swift - 区分单个 Sprite 上的多个物理体

ios - lottie 动画在 ios 中为 nil