动画removeFromSuperview

use*_*949 35 iphone cocoa-touch core-animation uiview ios

我想动画从子视图到超级视图的过渡.

我使用以下方式显示子视图:

[UIView beginAnimations:@"curlup" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[self.view addSubview:self.mysubview.view];
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

以上工作正常.它回到超级视图,我没有得到任何动画:

[UIView beginAnimations:@"curldown" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

我有什么不同的东西可以让子视图在移除时动画化吗?

Jos*_*phH 109

如果您向上定向iOS 4.0,则可以使用动画块:

[UIView animateWithDuration:0.2
     animations:^{view.alpha = 0.0;}
     completion:^(BOOL finished){ [view removeFromSuperview]; }];
Run Code Online (Sandbox Code Playgroud)

(上面的代码来自Apple的UIView文档)

  • 谢谢.这很简单. (3认同)

new*_*cct 27

我认为你需要这样做forView:self.view.superview,以便与你在添加时所做的事情保持一致,因为在这种情况下,self.view它是孩子,所以你需要在父母那样做.


Tal*_*ham 15

约瑟夫在Swift的回答:

UIView.animateWithDuration(0.2, animations: {view.alpha = 0.0}, 
                                completion: {(value: Bool) in
                                              view.removeFromSuperview()
                                            })
Run Code Online (Sandbox Code Playgroud)


nal*_*exn 5

尽管removeFromSuperview从动画完成块发送消息的方法在大多数情况下都可以正常工作,但有时无法阻止视图立即从视图层次结构中删除。

例如,MKMapView在收到 message 后删除其子视图removeAnnotations,并且在 API 中没有此消息的“动画”替代方案。

尽管如此,以下代码允许您在视图从超级视图中删除甚至解除分配后对视图的可视化克隆执行​​任何您喜欢的操作:

UIView * snapshotView = [view snapshotViewAfterScreenUpdates:NO];
snapshotView.frame = view.frame;
[[view superview] insertSubview:snapshotView aboveSubview:view];

// Calling API function that implicitly triggers removeFromSuperview for view
[mapView removeAnnotation: annotation];

// Safely animate snapshotView and release it when animation is finished
[UIView animateWithDuration:1.0
                     snapshotView.alpha = 0.0;
                 }
                 completion:^(BOOL finished) {
                     [snapshotView removeFromSuperview];
                 }];
Run Code Online (Sandbox Code Playgroud)