How can I simulate a beating heart?
Try this pulse animation:
CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.scale"]; theAnimation.duration=0.7; theAnimation.repeatCount=HUGE_VALF; theAnimation.autoreverses=YES; theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; theAnimation.toValue=[NSNumber numberWithFloat:0.7]; theAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; [self.heart.layer addAnimation:theAnimation forKey:@"animateOpacity"];
/**
Heart beating animation
*/
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, little more realistic animation
So far I have it so the heart changes size every second, but I want it to be more realistic.
The obvious suggestion is to use a sequence of images, to give you a "smooth" animation.