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

4

我目前正在尝试使用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{ get set }
设置play调用的循环行为。 默认为playOnce选项: playOnce:只播放一次动画,然后停止。 :loop:动画将从结尾循环到开头,直到停止。 :autoReverse:动画将前进,然后向后播放并循环,直到停止。 :repeat(amount):动画将在结束时循环播放到开始,最多重复amount次。 :repeatBackwards(amount):动画将向前播放,然后向后播放amount次。
没有.repeatBackwards(amount)选项。 有人知道如何使其正常工作吗?
5个回答

1

或者,如果您不想使用最后一帧,可以执行以下操作:

animationView.play(fromProgress: 1, toProgress: 0, loopMode: .playOnce) {}

1
这是如何为我工作的:

    //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)")

        }

    }

0

只需使用这个:

animationView.reverseAnimationSpeed();


0
private func playForward() {
    self.animationView.play() 
}

private func playReverse() {
    self.animationView.currentProgress = 1
    self.animationView.play(fromProgress: 1, toProgress: 0)
}

试试这个!


根据目前的写法,你的回答不够清晰。请编辑以添加更多细节,帮助其他人理解这如何回答所提出的问题。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - Community

-1

为了让你的动画只运行一次,你只需要删除引用循环的那一行代码(starAnimationView.loopMode = .autoReverse)。 你的代码已经很完美了,只剩下那一行。希望我能帮到你。


它已经有了starAnimationView.loopMode = .autoReverse。请检查代码。 - Sajjad Sarkoobi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接