how to add bounce animation to animateWithDuration?
Asked Answered
V

2

6

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

Vachil answered 13/1, 2015 at 16:38 Comment(1)
E
21

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.

Ever answered 13/1, 2015 at 17:3 Comment(1)
This is the best answer. (voted.) The animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion: method is tailor-made for what the OP is asking to do. I wrote a demo program on github that animates the hands of a clock using exactly this call and it gives a very realistic bounce look.Apprehension
P
0

One good solution is to create a custom layer for your view that overrides the addAnimation:forKey: method to include a custom timing function.

This answer goes into the specifics of how to do that.

Another option is to take a look at key frame animation. This question and answer covers that approach very well.

Pau answered 13/1, 2015 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.