timdonnelly / Advance

Physics-based animations for iOS, tvOS, and macOS.
http://timdonnelly.github.io/Advance/
BSD 2-Clause "Simplified" License
4.5k stars 207 forks source link

Missing callback when animator finished running. #42

Open strangeliu opened 5 years ago

strangeliu commented 5 years ago

use changedValue == targetValue is incorrect for some reason.

timdonnelly commented 5 years ago

Thanks for opening @strangeliu.

Can you please provide mode information on what you were trying to do, what you expected to happen, and what actually happened?

strangeliu commented 5 years ago

In the version v2.2.0, we can run an animation with AnimationRunner and set a callback when it finished running

let runner = AnimationRunner(animation: animation)
runner.onCompletion { _ in
    print("animation finished running")
}

After upgrade to version 3.0.0, The AnimationRunner is removed, so i use animator.animate(to: targetValue, duration: duration) to run an animation, but there's no way to know when the animation is finished running.

timdonnelly commented 5 years ago

Got it – would adding back an optional completion argument be helpful for the methods on Animator?


public enum CompletionReason {
    case finished
    case interrupted
}

public func animate(
    to finalValue: Value, 
    duration: Double, 
    timingFunction: TimingFunction = .swiftOut,
    completion: @escaping (CompletionReason) -> Void)

public func simulate<T>(
    using function: T, 
    initialVelocity: T.Value,
    completion: @escaping (CompletionReason) -> Void = { _ in }) where Value == T.Value, T : SimulationFunction

public func simulate<T>(
    using function: T,
    completion: @escaping (CompletionReason) -> Void = { _ in }) where Value == T.Value, T : SimulationFunction
timdonnelly commented 5 years ago

Most of the previously public API was unused, so v3 moves toward a much simpler model – the advent of types like UIViewPropertyAnimator mean that Advance is primarily useful for spring physics instead of general purpose animation. (Technically UIViewPropertyAnimator can use springs to control timing, but it's not as well suited to general purpose springs)

guidedways commented 3 years ago

Any update on this?