how to add bounce animation to animateWithDuration?

nick shmick picture nick shmick · Jan 13, 2015 · Viewed 8.2k times · Source

I have a simple animation that im performing in my scroll view delegate method scrollViewDidEndDragging.

It looks like this:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    NSLog(@"finger was lifted");

    [UIView animateWithDuration:1.0
                     animations:^{
                         self.homeLabel.frame = self.view.frame;
                    }];
}

Using this animation after lifting the finger the my homeLabel is coming from top, and i want to add it a bounce animation to the label, so when it comes from top, instead of landing smoothly it will have a nice bounce...how can i DO THAT? thanksss

Answer

rakeshbs picture rakeshbs · Jan 13, 2015

You can use the usingSpringWithDamping animation function.

[UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:5.0 options:UIViewAnimationOptionCurveLinear animations:^{
    self.homeLabel.frame = self.view.frame;
} completion:^(BOOL finished) {

}];

Adjusting the Spring Damping and Initial Spring Velocity can give you the effect you want.