Eliminar una subvista con animación

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.

Gracias,

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];
}

preguntado el 08 de noviembre de 11 a las 14:11

Don't forget about delegate ;) [UIView setAnimationDelegate:self]; -

3 Respuestas

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];}];

respondido 08 nov., 11:18

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];}];

respondido 08 nov., 11:18

oh, yes, my fault. I forget to remove one of the method. - Yongwei Xing

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

respondido 08 nov., 11:18

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.