From 4504c20313e13fa673667718f52127bbb249184b Mon Sep 17 00:00:00 2001 From: Brad Martin Date: Wed, 3 Nov 2021 21:30:49 -0500 Subject: [PATCH] feat: loop prop and public methods --- .../LottieViewModel.swift | 52 +++++++++++-------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/LottieTutorialWatchOS-SwiftUI WatchKit Extension/LottieViewModel.swift b/LottieTutorialWatchOS-SwiftUI WatchKit Extension/LottieViewModel.swift index 3199479..1de3edd 100644 --- a/LottieTutorialWatchOS-SwiftUI WatchKit Extension/LottieViewModel.swift +++ b/LottieTutorialWatchOS-SwiftUI WatchKit Extension/LottieViewModel.swift @@ -12,6 +12,9 @@ import SDWebImageLottieCoder class LottieViewModel: ObservableObject { @Published private(set) var image: UIImage = UIImage(named: "defaultIcon")! + /// Set false to disable looping the animation + var loop: Bool = true + // MARK: - Animation private var coder: SDImageLottieCoder? @@ -42,6 +45,26 @@ class LottieViewModel: ObservableObject { self.setupAnimation(with: data) } } + + /// Start playing animation + func play() { + playing = true + + animationTimer?.invalidate() + animationTimer = Timer.scheduledTimer(withTimeInterval: 0.05/speed, repeats: true, block: { (timer) in + guard self.playing else { + timer.invalidate() + return + } + self.nextFrame() + }) + } + + /// Pauses animation + func pause() { + playing = false + animationTimer?.invalidate() + } /// Decodify animation with given data /// - Parameter data: data of animation @@ -71,29 +94,16 @@ class LottieViewModel: ObservableObject { // make sure that current frame is within frame count // if reaches the end, we set it back to 0 so it loops if currentFrame >= coder.animatedImageFrameCount { - currentFrame = 0 + // check if we are looping, set to 0 if so + if self.loop == true { + currentFrame = 0 + } else { + // if we are playing once, basically reset everything + playing = false + animationTimer?.invalidate() + } } setImage(frame: currentFrame) } - - /// Start playing animation - private func play() { - playing = true - - animationTimer?.invalidate() - animationTimer = Timer.scheduledTimer(withTimeInterval: 0.05/speed, repeats: true, block: { (timer) in - guard self.playing else { - timer.invalidate() - return - } - self.nextFrame() - }) - } - - /// Pauses animation - private func pause() { - playing = false - animationTimer?.invalidate() - } }