An extension of CASpringAnimation to set parameter easily

Updated
Jan 22, 2021 5:05 AM
Created
Jan 22, 2021 5:04 AM
Tags
CocoaCoreAnimation
Keywords

extension CASpringAnimation {
  
  /**
   Creates an instance from damping and response.
   the response calucation comes from https://medium.com/@nathangitter/building-fluid-interfaces-ios-swift-9732bb934bf5
   */
  public convenience init(
    keyPath path: String?,
    damping: CGFloat,
    response: CGFloat,
    initialVelocity: CGFloat = 0
  ) {
    let stiffness = pow(2 * .pi / response, 2)
    let damp = 4 * .pi * damping / response
    
    self.init(keyPath: path)
    self.mass = 1
    self.stiffness = stiffness
    self.damping = damp
    self.initialVelocity = initialVelocity
    
  }
}

About describing velocity by 2D