
func addHeartBeatAnimation () { let beatLong: CABasicAnimation = CABasicAnimation(keyPath: "transform.scale") beatLong.fromValue = NSValue(CGSize: CGSizeMake(1, 1)) beatLong.toValue = NSValue(CGSize: CGSizeMake(0.7, 0.7)) beatLong.autoreverses = true beatLong.duration = 0.5 beatLong.beginTime = 0.0 let beatShort: CABasicAnimation = CABasicAnimation(keyPath: "transform.scale") beatShort.fromValue = NSValue(CGSize: CGSizeMake(1, 1)) beatShort.toValue = NSValue(CGSize: CGSizeMake(0.5, 0.5)) beatShort.autoreverses = true beatShort.duration = 0.7 beatShort.beginTime = beatLong.duration beatLong.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn ) let heartBeatAnim: CAAnimationGroup = CAAnimationGroup() heartBeatAnim.animations = [beatLong, beatShort] heartBeatAnim.duration = beatShort.beginTime + beatShort.duration heartBeatAnim.fillMode = kCAFillModeForwards heartBeatAnim.removedOnCompletion = false heartBeatAnim.repeatCount = FLT_MAX self.layer.addAnimation(heartBeatAnim, forKey: nil) }
Based on BCBlanka's answer, a bit more realistic animation
source share