Yongwei Xing
Yongwei Xing

Reputation: 13441

Remove a subview with ainimation

I am trying to remove a subview form a view with animation like below. However, when I click a button to run the code, the view is removed immediately. Does anyone know what happen here.

Thanks,

CGRect rect=[self.view viewWithTag:10].frame;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1];
        [UIView setAnimationDidStopSelector:@selector(removeLayer)];
        [[self.view viewWithTag:10] setFrame:CGRectMake(rect.origin.x, btn.frame.origin.y, rect.size.width, 0)];
        [UIView animateWithDuration:1.0 animations:^{[[self.view viewWithTag:10] setFrame:CGRectMake(rect.origin.x, btn.frame.origin.y, rect.size.width, 0)];} completion:^(BOOL finished){[[self.view viewWithTag:10] removeFromSuperview];}];
        [UIView commitAnimations];


-(void)removeLayer{
    [[self.view viewWithTag:10] removeFromSuperview];
}

Upvotes: 0

Views: 120

Answers (3)

Dancreek
Dancreek

Reputation: 9544

It looks like you are trying to use two different animation methods simultaneously. All you need is:

CGRect rect=[self.view viewWithTag:10].frame;

[UIView animateWithDuration:1.0 animations:^{[[self.view viewWithTag:10] setFrame:CGRectMake(rect.origin.x, btn.frame.origin.y, rect.size.width, 0)];} completion:^(BOOL finished){[[self.view viewWithTag:10] removeFromSuperview];}];

Upvotes: 1

yas375
yas375

Reputation: 3980

I think that you forgot to set animation delegate:

[UIView setAnimationDelegate:self];

In your code UIView doesn't know to which object it should send "removeLayer" selector

Upvotes: 0

jrturton
jrturton

Reputation: 119242

You are combining both types of animation API - I think the block-based one is returning immediately so your didStopSelector is being called straight away.

Try just using this part of it:

[UIView animateWithDuration:1.0 animations:^{[[self.view viewWithTag:10] setFrame:CGRectMake(rect.origin.x, btn.frame.origin.y, rect.size.width, 0)];} completion:^(BOOL finished){[[self.view viewWithTag:10] removeFromSuperview];}];

Upvotes: 1

Related Questions