16

我希望视图在通过添加到堆栈时淡入

[self.view addSubview:someSecondaryViewController.view];

如何为这个调用设置动画,以便视图淡入(淡出)?

4

3 回答 3

29

在设置动画之前将 alpha 设置为零,然后将 alpha 设置为 1。

[fadingView setAlpha:0.0];
[containerView addSubview:fadingView];
[UIView beginAnimations:nil context:nil];
[fadingView setAlpha:1.0];
[UIView commitAnimations];

在移除视图之前,只需将 alpha 设置为零即可。

顺便说一句,视图层次结构更像是一棵树而不是堆栈。

编辑:

如果在淡出视图时动画结束后没有其他清理,请使用:

[UIView setAnimationDelegate:fadingView];
[UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];

如果您已经设置了 didStopSelector,则在此处调用 removeFromSuperview。

于 2010-05-17T00:45:56.193 回答
21

您还可以在完成淡出动画后使用块从其超级视图中删除视图:

[UIView animateWithDuration:0.2
                 animations:^{viewOut.alpha = 0.0;}
                 completion:^(BOOL finished){[viewOut removeFromSuperview];}];
于 2011-03-17T10:13:47.157 回答
1

而在 Swift 中……</p>

someSecondaryViewController.view.alpha = 0.0
self.view.addSubview(someSecondaryViewController.view)
UIView.animate(withDuration: 0.2, animations: { self.someSecondaryViewController.view.alpha = 1.0 })

出去

UIView.animate(withDuration: 0.2, animations: { self.someSecondaryViewController.view.alpha = 0.0 }) { (done: Bool) in
    self.someSecondaryViewController.view.removeFromSuperview()
}
于 2016-06-16T14:38:13.967 回答