删除带动画的子视图

时间:2011-11-08 14:21:41

标签: iphone uiview

我正在尝试使用下面的动画从视图中删除子视图。但是,当我单击按钮运行代码时,视图将立即删除。有谁知道这里发生了什么。

谢谢,

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

3 个答案:

答案 0 :(得分:1)

您正在组合两种类型的动画API - 我认为基于块的动画API会立即返回,因此您的didStopSelector会立即被调用。

尝试使用它的这一部分:

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

答案 1 :(得分:1)

看起来您正在尝试同时使用两种不同的动画方法。您所需要的只是:

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

答案 2 :(得分:0)

我认为您忘了设置动画委托:

[UIView setAnimationDelegate:self];

在你的代码中,UIView不知道应该向哪个对象发送“removeLayer”选择器