From 4d6dc21d2f9c23532f9f40a9ce4eed182aba43bf Mon Sep 17 00:00:00 2001 From: Pim Date: Fri, 14 Jun 2024 16:23:07 +0200 Subject: [PATCH] Always perform group animations in defined order --- .../AnimationPlanner/Animations/Group.swift | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/AnimationPlanner/Animations/Group.swift b/Sources/AnimationPlanner/Animations/Group.swift index 812c5a0..6b2d461 100644 --- a/Sources/AnimationPlanner/Animations/Group.swift +++ b/Sources/AnimationPlanner/Animations/Group.swift @@ -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() {