[IOS] Spring Animations (elastic animation)

  This interface shows how a spring animation can be created by specifying a “damping” (bounciness) and “response” (speed).

  This shows how to create interactive animation spring by specifying "damping" (flexible) and "Response" (speed).

  

  Key Features (Key Features)

  1. Uses "design-friendly" parameters. (Use friendly parameters).
  2. No concept of animation duration. (No animation concept of duration).
  3. Easily interruptible. (Easy to break).

  Design Theory (Design Theory)

  Springs make great animation models because of their speed and natural appearance. A spring animation starts incredibly quickly, spending most of its time gradually approaching its final state. This is perfect for creating interfaces that feel responsive—they spring to life!

  We can achieve good elasticity animated models, because of their speed and natural look. Start elastic animation speed very quickly, most of the time edging closer to its final state. It feels responsive interface is perfect for creating.

  A few additional reminders when designing spring animations:

  Some additional tips when designing spring animation:

  1. Springs do not have to be springy Using a damping value of 1 will create an animation that slowly comes to rest without any bounciness Most animations should use a damping value of 1. Translation: The spring is not necessarily elastic. Use damping value of 1 will create an animation, it will slowly cease, without any flexibility, you should use most of the animation damping value 1.
  2. . Try to avoid thinking about duration In theory, a spring never fully comes to rest, and forcing a duration on the spring can cause it to feel unnatural Instead, play with the damping and response values ​​until it feels right translation: Try to avoid consider duration. In theory, the spring is never completely still, forced to spring for some time will make it feel unnatural. In contrast, the use of damping and response value, until it feels far right.

  3. Interruption is critical. Because springs spend so much of their time close to their final value, users may think the animation has completed and will try to interact with it again. Interruption is crucial, because the elastic spent so much time close to their ultimate value, users might think that the animation has been completed, and will try to interact with it again.

   Critical Code (key code)

  In UIKit, we can create a spring animation with a UIViewPropertyAnimatorand a UISpringTimingParameters object. Unfortunately, there is no initializer that just takes a damping and response. The closest we can get is the UISpringTimingParameters initializer that takes a mass, stiffness, damping, and initial velocity.

  In UIKit, we can create animations with a spring UIViewPropertyAnimator and UISpringTimingParameters objects. Unfortunately, there is no initializer only accept damping and response. Closest value we can get is UISpringTimingParameters initializer, it has a mass, stiffness, damping and initial velocity.

UISpringTimingParameters(mass: CGFloat, stiffness: CGFloat, damping: CGFloat, initialVelocity: CGVector)

  We would like to create a convenience initializer that takes a damping and response, and maps it to the required mass, stiffness, and damping.

  We want to create a convenient initialization that accepts damping and response, and map it to the mass, stiffness and damping required.
  With a little bit of physics, we can derive the equations we need:

  With a little knowledge of physics, we can derive the equation we need:

 

  With this result, we can create our own UISpringTimingParameters with exactly the parameters we desire.

  With this result, we can create our own UISpringTimingParameters, these parameters exactly what we want.

extension UISpringTimingParameters {
    convenience init(damping: CGFloat, response: CGFloat, initialVelocity: CGVector = .zero) {
        let stiffness = pow(2 * .pi / response, 2)
        let damp = 4 * .pi * damping / response
        self.init(mass: 1, stiffness: stiffness, damping: damp, initialVelocity: initialVelocity)
    }
}

 

Guess you like

Origin www.cnblogs.com/xjf125/p/12150345.html