In the previous view property animation article, we first provided an initial state of the view, then changed some properties of the view in the animation block to make it the final state we wanted, and provided some other parameters such as the animation duration. Finally, UIKit automatically generated the corresponding animation for us.

The previous view movement was pretty simple, in a certain direction, from one point to another. In life, when an object is attached to a spring, the motion of the object is a relatively complex elastic motion. UIView provides an animation method whose time corresponds to the motion curve of the physical spring. See the following figure for details:

The default animation curve is relatively smooth, and the Spring animation curve starts fast and ends slow.

For example, moving from point A to point B is smooth if you use the animation method in the previous article. If we add a little Spring effect to the animation this time, the animation will show the View oscillating at point B for a while and eventually coming back to point B. It’s like there’s a spring at B that’s holding the View.

Let’s go directly to the API:

    [UIView animateWithDuration:NSTimeInterval)duration

                          delay:NSTimeInterval)delay

         usingSpringWithDamping:(CGFloat)dampingRatio

          initialSpringVelocity:(CGFloat)velocity

                        options:(UIViewAnimationOptions)options

                     animations:^{

                         <#code#>

                     }

                     completion:^(BOOL finished) {

                         <#code#>

                     }

     ];

Copy the code

Duration, delay, animations, and Completion parameters are the same as in the previous method. The following two parameters are introduced:

  • DampingRatio: The damping coefficient ranges from 0.0 to 1.0. The smaller the value, the more severe the Spring vibration and the more obvious the Spring effect.

  • Velocity: indicates the speed. The higher the value, the faster the movement. With a value of 1.0, this speed is the speed at which the entire animation distance is covered in 1 second. Larger or smaller values make the view faster or slower when it first reaches its destination. The image above is more direct:

Specific use or need to actually write some code to feel.

Spring animation is widely used in iOS system. Spring animation can not only animate the position properties of view, such as size, but also be applied to other animation properties of UIView.