Skip to content

Commit

Permalink
Merge pull request airbnb#910 from daemedeor/lottie-forever
Browse files Browse the repository at this point in the history
Enhancement: Modified to allow for defined repeat counts
  • Loading branch information
buba447 committed May 29, 2019
2 parents 2e6b282 + 8998253 commit 2ee6546
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ 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.
#
#### Is Animation Playing
```swift
Expand Down
37 changes: 35 additions & 2 deletions lottie-swift/src/Public/Animation/AnimationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ public enum LottieLoopMode {
case loop
/// Animation will play forward, then backwards and loop until stopped.
case autoReverse
/// Animation will loop from end to beginning up to defined amount of times.
case `repeat`(Float)
/// Animation will play forward, then backwards a defined amount of times.
case repeatBackwards(Float)
}

extension LottieLoopMode: Equatable {
public static func == (lhs: LottieLoopMode, rhs: LottieLoopMode) -> Bool {
switch (lhs, rhs) {
case (.repeat(let lhsAmount), .repeat(let rhsAmount)),
(.repeatBackwards(let lhsAmount), .repeatBackwards(let rhsAmount)):
return lhsAmount == rhsAmount
case (.playOnce, .playOnce),
(.loop, .loop),
(.autoReverse, .autoReverse):
return true
default:
return false
}
}
}

@IBDesignable
Expand Down Expand Up @@ -777,8 +797,21 @@ final public class AnimationView: LottieView {
layerAnimation.duration = TimeInterval(duration)
layerAnimation.fillMode = CAMediaTimingFillMode.both

layerAnimation.repeatCount = loopMode == .playOnce ? 1 : HUGE
layerAnimation.autoreverses = loopMode == .autoReverse ? true : false
switch loopMode {
case .playOnce:
layerAnimation.repeatCount = 1
case .loop:
layerAnimation.repeatCount = HUGE
case .autoReverse:
layerAnimation.repeatCount = HUGE
layerAnimation.autoreverses = true
case let .repeat(amount):
layerAnimation.repeatCount = amount
case let .repeatBackwards(amount):
layerAnimation.repeatCount = amount
layerAnimation.autoreverses = true
}

layerAnimation.isRemovedOnCompletion = false
if timeOffset != 0 {
let currentLayerTime = viewLayer?.convertTime(CACurrentMediaTime(), from: nil) ?? 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public final class CompatibleAnimationView: UIView {
}

@objc
public var loopAnimation: Bool = false {
public var loopAnimationCount: CGFloat = 0 {
didSet {
animationView.loopMode = loopAnimation ? .loop : .playOnce
animationView.loopMode = loopAnimationCount == -1 ? .loop : .repeat(Float(loopAnimationCount))
}
}

Expand Down

0 comments on commit 2ee6546

Please sign in to comment.