facebookarchive / pop

An extensible iOS and OS X animation library, useful for physics-based interactions.
Other
19.66k stars 2.88k forks source link

I want to know POPSpringAnimation's duration time. #134

Closed WilliamZang closed 10 years ago

WilliamZang commented 10 years ago

Can I get how long the POPSpringAnimation will last? How can I specify a duration time?

grp commented 10 years ago

Spring animations are physics simulations, so there isn't a real concept of a duration. For example, you can change the toValue of the spring animation while it's active, which could redirect the animation to a new place and completely change the duration.

What do you need the duration for? We might be able to suggest something else you could use.

WilliamZang commented 10 years ago

@grp I want to implement viewcontroller transition, so it need a duration time for method - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext

grp commented 10 years ago

Ah. I'm not sure how those work with Pop, since they're fairly closely tied to the UIView animation system. Perhaps try returning 0 and doing the animation with Pop yourself?

WilliamZang commented 10 years ago

I think it may work with pop, the only thing is duration time.

grp commented 10 years ago

Pop spring animations don't conceptually have a duration, so I'm not sure you can directly integrate them with this system. Have you tried estimating a time that makes sense for your transition?

WilliamZang commented 10 years ago

Yes, estimating a time is OK. All right, I'll let fade out animation have a specify duration time, but let fade in animation use estimating time.

toulouse commented 10 years ago

To clarify for anyone else interested in why - the way spring animations work is that they oscillate, damped according to their configuration options, until they are within some threshold and are then considered to have "converged".

In order to calculate a time, you could theoretically try to run the physics simulation's mathematical calculation yourself until it converges. Using that, you would determine how many ticks of the animator it would take to converge. Pop's animator is powered by CADisplayLink, so a tick would be roughly once per frame, or 16.67ms.

Unfortunately, this would still, at best, be an estimate, as any frame drops could cause this calculation to fail in the face of a time deadline that wasn't affected by frame drops. Additionally, as the developer, you would need to be absolutely sure the spring animation's parameters did not change during this time-dependent operation, as that too would invalidate the calculation.

tl;dr: Springs can't tell you ahead of time when they will converge, only that they have converged. Unless you can accurately predict frame drops (or don't have any at all), you can't accurately predict the exact length of the spring animation, either. In situations which require a hard time limit, estimation is a good approach.

WilliamZang commented 10 years ago

OK,I see it.