UIButton Heartbeat Animation

Swift 5 code, works without the pause between pulses:

    let pulse = CASpringAnimation(keyPath: "transform.scale")
    pulse.duration = 0.4
    pulse.fromValue = 1.0
    pulse.toValue = 1.12
    pulse.autoreverses = true
    pulse.repeatCount = .infinity
    pulse.initialVelocity = 0.5
    pulse.damping = 0.8
    switchButton.layer.add(pulse, forKey: nil)

This works perfectly. The damping and spring need to be tweaked a little bit but this solves the problem. removeAllAnimations() clears the animation and returns the button to it's normal state.

button.userInteractionEnabled = true
button.enabled = true

let pulse1 = CASpringAnimation(keyPath: "transform.scale")
pulse1.duration = 0.6
pulse1.fromValue = 1.0
pulse1.toValue = 1.12
pulse1.autoreverses = true
pulse1.repeatCount = 1
pulse1.initialVelocity = 0.5
pulse1.damping = 0.8

let animationGroup = CAAnimationGroup()
animationGroup.duration = 2.7
animationGroup.repeatCount = 1000
animationGroup.animations = [pulse1]

button.layer.addAnimation(animationGroup, forKey: "pulse")

This post was very helpful: CAKeyframeAnimation delay before repeating


I created something like this:

    let animation = CAKeyframeAnimation(keyPath: "transform.scale")

    animation.values = [1.0, 1.2, 1.0]
    animation.keyTimes = [0, 0.5, 1]
    animation.duration = 1.0
    animation.repeatCount = Float.infinity
    layer.add(animation, forKey: "pulse")