How to add bounce effect to UIView animateWithDuration

Eric picture Eric · May 6, 2015 · Viewed 10.1k times · Source

I am trying to add a bounce effect to the animation below. Here is my code:

[UIView animateWithDuration:1.0
                       delay:.0
      usingSpringWithDamping:0.5
       initialSpringVelocity:2.0
                     options:UIViewAnimationOptionCurveEaseOut
                  animations:^{
                      // Coming from a value of CGAffineTransformMakeScale(0.001, 1.0)
                      self.myView.transform = CGAffineTransformMakeScale(1.0, 1.0);
                  }completion:nil
          ];

It's not working correctly. It becomes wider at the end of the animation then it goes back to normal. I want the width to bounce to a lesser value than 1.0, not more than 1.0.

Answer

Vijay yadav picture Vijay yadav · May 6, 2015

ust for future code reuse and keeping code clean

popUp.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);

[self.view addSubview:popUp];

[UIView animateWithDuration:0.3/1.5 animations:^{
    popUp.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.3/2 animations:^{
        popUp.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3/2 animations:^{
            popUp.transform = CGAffineTransformIdentity;                            
        }];
    }];
}];