Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove uiview with animation

I added an UIView with some animation by using the following code:

CGRect a = CGRectMake(10,10,295,460);
UIView *customView = [[UIView alloc]initWithFrame:a];
customView.tag=10;
[self.tableView addSubview:customView];

[UIView animateWithDuration:1.0
                 animations:
 ^{
     customView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
 }
 ];

When I try to remove the UIView with animation, the animation seems not working. The view is just getting removed without any animation.

Here is what I have to remove the UIView with animation:

UIView *removeView=[self.tableView viewWithTag:10] ;

        removeView .transform = CGAffineTransformMakeScale(1.0f, 1.0f);


        [removeView removeFromSuperview];

[UIView animateWithDuration:1.5
                 animations:
 ^{
     removeView .transform = CGAffineTransformMakeScale(0.0f, 0.0f);
 }
 ];

I tried by placing the [UIView animateWithDuration:1.5 ... block before and after the [removeView removeFromSuperview], but none gave me the animation. Kindly help me out in this.


2 Answers

[UIView animateWithDuration:1.5 animations:^ {
     removeView.transform = CGAffineTransformMakeScale(0.0f, 0.0f);
} completion:^(BOOL finished) {
     [removeView removeFromSuperview]; 
}];
like image 187
LDNZh Avatar answered Sep 16 '25 11:09

LDNZh


Some changes:

UIView *removeView=[self.tableView viewWithTag:10] ;

    removeView .transform = CGAffineTransformMakeScale(1.0f, 1.0f);

    [removeView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.5];

    [UIView animateWithDuration:1.5
                     animations:
     ^{
         removeView .transform = CGAffineTransformMakeScale(0.01f, 0.01f);
     }
     ];

Try setting your final transform to something small but nonzero.

like image 38
Tanuj Avatar answered Sep 16 '25 11:09

Tanuj