Skip to content

Commit

Permalink
Always perform group animations in defined order
Browse files Browse the repository at this point in the history
  • Loading branch information
PimCoumans committed Jun 14, 2024
1 parent ad6a919 commit 4d6dc21
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Sources/AnimationPlanner/Animations/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ public struct Group: SequenceAnimatable {
extension Group: PerformsAnimations {

public func animate(delay: TimeInterval, completion: ((Bool) -> Void)?) {
var sortedAnimations = animations
.sorted { $0.duration < $1.duration }
.compactMap { $0 as? PerformsAnimations }

guard !sortedAnimations.isEmpty else {
let animations = animations.compactMap { $0 as? PerformsAnimations }
guard let longestDuration = animations.map(\.duration).max() else {
completion?(true)
return
}
var hasAddedCompletionHandler: Bool = false

let longestAnimation = sortedAnimations.removeLast()

sortedAnimations.forEach { animation in
animation.animate(delay: delay, completion: nil)
for animation in animations {
if animation.duration >= longestDuration, !hasAddedCompletionHandler {
hasAddedCompletionHandler = true
animation.animate(delay: delay, completion: completion)
} else {
animation.animate(delay: delay, completion: nil)
}
}
longestAnimation.animate(delay: delay, completion: completion)
}

public func stop() {
Expand Down

0 comments on commit 4d6dc21

Please sign in to comment.