我目前正在尝试使用 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 toplayOnce
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/