如何更改模态 UIViewController 的动画样式? [英] How can I change the animation style of a modal UIViewController?
问题描述
我目前正在显示一个这样的 UIViewController:
I'm currently displaying a UIViewController like this:
[[self navigationController] presentModalViewController:modalViewController animated:YES];
并像这样隐藏它:
[self.navigationController dismissModalViewControllerAnimated:YES];
动画是从底部向上滑动"...然后向下滑动.如何更改动画样式?我可以让它淡入/淡出吗?
The animation is "slide up from the bottom"... then slide back down. How can I change the animation style? Can I made it fade in/out?
干杯!
推荐答案
Marcus Zarra 在 SDK 邮件列表上发布了一个很好的解决方案:
Marcus Zarra posted a great solution to this on the SDK mailing list:
UIViewController *controller = [[[MyViewController alloc] init] autorelease];
UIViewAnimationTransition trans = UIViewAnimationTransitionCurlUp;
[UIView beginAnimations: nil context: nil];
[UIView setAnimationTransition: trans forView: [self window] cache: YES];
[navController presentModalViewController: controller animated: NO];
[UIView commitAnimations];
有翻转和翻页的过渡.如果您设置为淡入淡出,可以尝试调整新视图的 alpha:
There are transitions for flipping and page-curling. If you are set on fading, can try adjusting your new view's alpha:
UIViewController *controller = [[[MyViewController alloc] init] autorelease];
controller.view.alpha = 0.0;
[navController presentModalViewController: controller animated: NO];
[UIView beginAnimations: nil context: nil];
controller.view.alpha = 1.0;
[UIView commitAnimations];
但是,您可能想要的是交叉淡入淡出,或者至少是淡入淡出.当 UINavigationController 切换到新视图时,它会删除旧视图.对于这种效果,您最好向现有的 UIViewController 添加一个新视图并随着时间的推移逐渐淡出它的 alpha.
However, what you probably want is a crossfade, or at least a fade-over. When the UINavigationController switches to a new view, it removes the old one. For this effect, you're probably better off just adding a new view to your existing UIViewController and fading its alpha in over time.
注意:如果您不在您的应用委托中,[self window] 将无法工作.使用 self.view.window ,感谢 user412500 的帖子指出这一点.
Note: If you are not in your app delegate [self window] will not work. Use self.view.window , thanks to user412500's post for pointing this out.
这篇关于如何更改模态 UIViewController 的动画样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!