title: UIView Animating a 2D Bouncing Ball (Squash & Stretch) in iOS
body: <p>Looking for Animating a view like Bouncing Ball using Squash &amp; Stretch, in cocoa using core animation...</p>
  
  <p>1 - Bouncing Ball Animation</p>
  
  <p><img src="https://i.stack.imgur.com/7WQDB.gif" alt="Bouncing Ball Animation">
  ;;;;</p>
  
  <pre><code>CABasicAnimation *animMoveUp = [CABasicAnimation animationWithKeyPath:@"position"];
  animMoveUp.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
  animMoveUp.fromValue   =   [NSValue valueWithCGPoint:_bottom];
  animMoveUp.toValue     =   [NSValue valueWithCGPoint:_top];
  
  CABasicAnimation *scaleAnim1 = [CABasicAnimation animationWithKeyPath:@"transform"];
  scaleAnim1.toValue      = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.5, 2.0, 1.0)];
  scaleAnim1.toValue      =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
  
  CABasicAnimation *bounceAnim1 = [CABasicAnimation animationWithKeyPath:@"transform"];
  bounceAnim1.fromValue   = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
  bounceAnim1.toValue     = [NSValue valueWithCATransform3D:CATransform3DMakeScale(3.0, 1.0, 1.0)];
  
  CABasicAnimation *animMoveDown = [CABasicAnimation animationWithKeyPath:@"position"];
  animMoveDown.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  animMoveDown.fromValue   =   [NSValue valueWithCGPoint:_top];
  animMoveDown.toValue     =   [NSValue valueWithCGPoint:_bottom];
  
  
  CABasicAnimation *scaleAnim2 = [CABasicAnimation animationWithKeyPath:@"transform"];
  scaleAnim2.fromValue    =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.5, 2.0, 1.0)];
  scaleAnim2.toValue      =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
  
  CABasicAnimation *bounceAnim2 = [CABasicAnimation animationWithKeyPath:@"transform"];
  bounceAnim2.fromValue   =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
  bounceAnim2.toValue     =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(3.0, 1.0, 1.0)];
  
  CABasicAnimation *animMoveUp1 = [CABasicAnimation animationWithKeyPath:@"position"];
  animMoveUp1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  animMoveUp1.fromValue   =   [NSValue valueWithCGPoint:_bottom];
  animMoveUp1.toValue     =   [NSValue valueWithCGPoint:_top];
  
  CABasicAnimation *scaleAnim3 = [CABasicAnimation animationWithKeyPath:@"transform"];
  scaleAnim3.fromValue    =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.5, 2.0, 1.0)];
  scaleAnim3.toValue      =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
  
  CABasicAnimation *bounceAnim3 = [CABasicAnimation animationWithKeyPath:@"transform"];
  bounceAnim3.fromValue   =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
  bounceAnim3.toValue     =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(3.0, 1.0, 1.0)];
  
  CABasicAnimation *animMoveDown2 = [CABasicAnimation animationWithKeyPath:@"position"];
  animMoveDown2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  animMoveDown2.fromValue   =   [NSValue valueWithCGPoint:_top];
  animMoveDown2.toValue     =   [NSValue valueWithCGPoint:_bottom];
  
  CABasicAnimation *scaleAnim4 = [CABasicAnimation animationWithKeyPath:@"transform"];
  scaleAnim4.fromValue    =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.5, 2.0, 1.0)];
  scaleAnim4.toValue      =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
  
  CABasicAnimation *bounceAnim4 = [CABasicAnimation animationWithKeyPath:@"transform"];
  bounceAnim4.fromValue   =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.0, 2.0, 1.0)];
  bounceAnim4.toValue     =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(3.0, 1.0, 1.0)];
  
  CAAnimationGroup *animGroup = [CAAnimationGroup animation];
  
  animGroup.animations = @[animMoveUp,    scaleAnim1,     bounceAnim1,
                           animMoveDown,  scaleAnim2,     bounceAnim2,
                           animMoveUp1,   scaleAnim3,     bounceAnim3,
                           animMoveDown2, scaleAnim4,     bounceAnim4];
  
  animGroup.duration      =   2.5;
  animGroup.delegate      =   self;
  animGroup.fillMode      =   kCAFillModeForwards;
  
  animGroup.removedOnCompletion = NO;
  
  [_imgView.layer addAnimation:animGroup forKey:nil];
  </code></pre>