我希望视图在通过添加到堆栈时淡入
[self.view addSubview:someSecondaryViewController.view];
如何为这个调用设置动画,以便视图淡入(淡出)?
我希望视图在通过添加到堆栈时淡入
[self.view addSubview:someSecondaryViewController.view];
如何为这个调用设置动画,以便视图淡入(淡出)?
在设置动画之前将 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。
您还可以在完成淡出动画后使用块从其超级视图中删除视图:
[UIView animateWithDuration:0.2
animations:^{viewOut.alpha = 0.0;}
completion:^(BOOL finished){[viewOut removeFromSuperview];}];
而在 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()
}