如何用动画删除uiview

时间:2015-03-23 13:31:32

标签: ios objective-c animation uiview

我使用以下代码添加了一个带有动画的UIView

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

当我尝试使用动画删除UIView时,动画似乎无效。视图刚刚被移除而没有任何动画。

以下是我必须使用动画删除UIView

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

我尝试在[UIView animateWithDuration:1.5 ...之前和之后放置[removeView removeFromSuperview]块,但没有人给我动画。请帮助我。

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

一些变化:

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

尝试将最终转换设置为小但非零的。

答案 2 :(得分:0)

尝试等待删除视图,直到动画结束:

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

答案 3 :(得分:0)

尝试这个,它对我有用:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5f];
removeView.transform = CGAffineTransformMakeTranslation(removeView.frame.origin.x,1000.0f + (removeView.frame.size.height/2));
aview.alpha = 0;
[UIView commitAnimations];

答案 4 :(得分:0)

请参阅Apple的以下示例。

[UIView transitionWithView:containerView
           duration:0.2
           options:UIViewAnimationOptionTransitionFlipFromLeft
           animations:^{ [fromView removeFromSuperview]; [containerView addSubview:toView]; }
           completion:NULL];

上面的代码为指定的容器视图创建了一个翻转过渡。在转换的适当位置,将删除一个子视图,并将另一个子视图添加到容器视图中。这使得新视图看起来好像被新的子视图翻转到了原位,但实际上它只是使用新配置动画回原点。

可能是您需要管理的动画类型。

参见: - https://developer.apple.com/documentation/uikit/uiview/1622574-transition