Animate a view zoom-bouncing in?
Too many complicated answers ð. It's much easier to do it as of 2017 (Swift 3/4).
Swift 4-
yourview.transform = yourview.transform.scaledBy(x: 0.001, y: 0.001)
UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.3, options: .curveEaseInOut, animations: {
yourview.transform = CGAffineTransform.identity.scaledBy(x: 1.0, y: 1.0)
}, completion: nil)
Note the usingSpringWithDamping
parameter. This parameter dictates how much bounce/spring effect you want and allows values from 0 to 1. The lower the value the more the bounce effect. Also, if you do not like the scaleBy
effect then you can simply use good old frames to show a expanding and bouncing UIView
.
Just refactored the code by Amit Singh using blocks, which makes it much simpler and readable.
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;
}];
}];
}];
This can be done in a much simpler way these days:
view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[UIView animateWithDuration:0.5 delay:0.2 usingSpringWithDamping:0.6f initialSpringVelocity:1.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
} completion:^(BOOL finished) {
view.transform = CGAffineTransformIdentity;
}];
write this code when you want to trigger this animation
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;
}];
}];
}];
SWIFT 5.0
selectView.transform =
CGAffineTransform.identity.scaledBy(x: 0.001, y: 0.001)
view.addSubview(selectView)
UIView.animate(withDuration: 0.3 / 1.5, animations: {
selectView.transform =
CGAffineTransform.identity.scaledBy(x: 1.1, y: 1.1)
}) { finished in
UIView.animate(withDuration: 0.3 / 2, animations: {
selectView.transform = .identity.scaledBy(x: 0.9, y: 0.9)
}) { finished in
UIView.animate(withDuration: 0.3 / 2, animations: {
selectView.transform = CGAffineTransform.identity
})
}
}
This is updated code (from fabio.cionini) as it is accepted answer so updating to latest.