Inside a UIView animation block, is there a way to get the current animation's duration?
[UIView animateWithDuration:1.0 animations:^{
// float duration = ?
}];
The time that an animation takes to complete one cycle. This may be specified in either seconds ( s ) or milliseconds ( ms ).
To run your animation effect at a faster or slower pace, change the Duration setting. On the slide, click the text or object that contains the animation effect that you want to set the speed for. On the Animations tab, in the Duration box, enter the number of seconds that you want the effect to run.
Duration: You can specify the duration of an animation. The default length is 300 ms.
Possible Values For example, a value of 5s would result in the animation taking 5 seconds to complete. By default the value is 0s , meaning that the animation cycle is immediate (i.e. you would not see any animation).
If you are targeting iOS 9.0
or later, then you are looking for a class property of UIView
, called inheritedAnimationDuration
.
Usage:
let currentAnimationDuration = UIView.inheritedAnimationDuration
From Apple docs:
Summary
Returns the inherited duration of the current animation.
Discussion
This method only returns a non-zero value if called within a
UIView
animation block.
TL;DR: use CALayer -actionForKey:, not -animationForKey:
@Dimitri Bouniol 's answer didn't work for me when called from an an affected setter inside an animation block. The reason, from my understanding, is that UIView's animation system sets up state before starting the actual animation (and calls setters before starting the actual animation). What worked for me though was calling the similar -actionForKey: method on the layer. The action returned did have the proper duration set, and can be used as it is in his answer.
CAAnimation *animation = (CAAnimation *)[self.layer actionForKey@"position"]; // or property of interest
[CATransaction begin];
[CATransaction setAnimationDuration:animation.duration];
[CATransaction setAnimationTimingFunction:animation.timingFunction];
// CALayer animation here
[CATransaction commit];
[CATransaction animationDuration]
is what you're looking for
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With