ios 动画 removeFromSuperview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2004313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Animating removeFromSuperview
提问by user230949
I'd like to animate the transition from a subview back to the super view.
我想动画从子视图回到超级视图的过渡。
I display the subview using:
我使用以下方法显示子视图:
[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];
The above works fine. It's going back to the super view that I don't get any animation:
以上工作正常。回到超级视图,我没有得到任何动画:
[UIView beginAnimations:@"curldown" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
Is there something different I should be doing to get the subview to animate when removed?
我应该做些什么不同的事情来让子视图在删除时设置动画?
采纳答案by newacct
I think you need to do forView:self.view.superview
instead, to be consistent with what you are doing when you are adding, because in this case the self.view
is the child, and so you would need to do it on the parent.
我认为你需要做的forView:self.view.superview
是,在添加时与你正在做的事情保持一致,因为在这种情况下self.view
是孩子,所以你需要在父母身上做。
回答by JosephH
If you're targeting iOS 4.0 upwards you can use animation blocks instead:
如果您的目标是 iOS 4.0 以上,您可以使用动画块:
[UIView animateWithDuration:0.2
animations:^{view.alpha = 0.0;}
completion:^(BOOL finished){ [view removeFromSuperview]; }];
(above code comes from Apple's UIView documentation)
(以上代码来自Apple 的 UIView 文档)
回答by Tal Haham
JosephH answer in Swift:
JosephH 用 Swift 回答:
UIView.animateWithDuration(0.2, animations: {view.alpha = 0.0},
completion: {(value: Bool) in
view.removeFromSuperview()
})
回答by nalexn
Although approach with sending removeFromSuperview
message from animation completion block works fine for most cases, sometimes there is no way to prevent a view from immediate removal from view hierarchy.
尽管removeFromSuperview
从动画完成块发送消息的方法在大多数情况下都可以正常工作,但有时无法阻止视图立即从视图层次结构中删除。
For example, MKMapView
removes its subviews after it receives message removeAnnotations
, and there is no "animated" alternative for this message in the API.
例如,MKMapView
在收到 message 后删除其子视图removeAnnotations
,并且在 API 中没有此消息的“动画”替代方案。
Nonetheless the following code allows you to do whatever you like with a visual clone of a view after it's been removed from superview or even deallocated:
尽管如此,以下代码允许您在视图从超级视图中删除甚至解除分配后对视图的可视化克隆执行任何您喜欢的操作:
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];
}];
回答by De Zheng
Example below:
下面的例子:
func removeSpinningGear(cell: LocalSongsCollectionViewCell) {
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveLinear, animations: {
cell.spinningGearBtn.alpha = 0.0
}) { _ in
cell.spinningGearBtn.removeFromSuperview()
}
}