over 9 years ago
Hi Readers,
In our app we usually make custom alerts which we need to animate at the time adding it on its superview.
To achieve it with you custom view make a category like this :
Put this code into your .h file of category named as UIView+AnimateView :
After that use following code in your .m file:
- @implementation UIView (AnimateView)
- - (void)addSubviewWithBounce:(UIView*)subview
- {
- subview.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
- [self addSubview:subview];
- [UIView animateWithDuration:0.3/1.5 animations:^{
- CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
- } completion:^(BOOL finished) {
- [UIView animateWithDuration:0.3/2 animations:^{
- subview.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
- } completion:^(BOOL finished) {
- [UIView animateWithDuration:0.3/2 animations:^{
- subview.transform = CGAffineTransformIdentity;
- }];
- }];
- }];
- }
- - (void)removeSubviewWithAnimation:(UIView*)subview{
- subview.transform = CGAffineTransformIdentity;
- [UIView animateWithDuration:0.3/1.5 animations:^{
- subview.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
- } completion:^(BOOL finished) {
- [subview removeFromSuperview];
- }];
- }
- @end
@implementation UIView (AnimateView) - (void)addSubviewWithBounce:(UIView*)subview { subview.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001); [self addSubview:subview]; [UIView animateWithDuration:0.3/1.5 animations:^{ CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1); } completion:^(BOOL finished) { [UIView animateWithDuration:0.3/2 animations:^{ subview.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9); } completion:^(BOOL finished) { [UIView animateWithDuration:0.3/2 animations:^{ subview.transform = CGAffineTransformIdentity; }]; }]; }]; } - (void)removeSubviewWithAnimation:(UIView*)subview{ subview.transform = CGAffineTransformIdentity; [UIView animateWithDuration:0.3/1.5 animations:^{ subview.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001); } completion:^(BOOL finished) { [subview removeFromSuperview]; }]; } @end
Just import this category on any controller and call these function like :
Thanks. Happy coding
Can you help out the community by solving one of the following Illustrator problems?
Do activity (Answer, Blog) > Earn Rep Points > Improve Rank > Get more opportunities to work and get paid!
For more topics, questions and answers, please visit the Tech Q&A page.
0 Comment(s)