Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform Group’s animations in defined order #22

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 2 additions & 4 deletions Sources/AnimationPlanner/Protocols/PerformsAnimations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import UIKit

/// Creates actual `UIView` animations for all animation structs. Implement ``animate(delay:completion:)`` to make sure any custom animation creates an actual animation.
/// Use the default implementation of ``timingParameters(leadingDelay:)-2swvd`` to get the most accurate timing parameters for your animation so any set delay isn't missed.
public protocol PerformsAnimations {
public protocol PerformsAnimations: Animatable {
/// Perform the actual animation
/// - Parameters:
/// - delay: Any delay accumulated (from preceding ``Wait`` structs) leading up to the animation.
Expand All @@ -22,13 +22,11 @@ public protocol PerformsAnimations {
extension PerformsAnimations {

public func timingParameters(leadingDelay: TimeInterval) -> (delay: TimeInterval, duration: TimeInterval) {
var parameters = (delay: leadingDelay, duration: TimeInterval(0))
var parameters = (delay: leadingDelay, duration: duration)

if let delayed = self as? DelayedAnimatable {
parameters.delay += delayed.delay
parameters.duration = delayed.originalDuration
} else if let animation = self as? Animatable {
parameters.duration = animation.duration
}
return parameters
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/AnimationPlanner/RunningSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ extension RunningSequence {
}

remainingAnimations = Array(impendingAnimations.dropFirst())
let duration = (animation as? Animatable)?.duration ?? 0
let duration = animation.duration
let completionDuration = duration + leadingDelay

let startTime = CACurrentMediaTime()
Expand Down
Loading