如何更改模态UIViewController的动画样式?


69

我目前正在显示这样的UIViewController:

[[self navigationController] presentModalViewController:modalViewController animated:YES];

并像这样隐藏它:

[self.navigationController dismissModalViewControllerAnimated:YES];

动画是“从底部向上滑动” ...然后向下滑动。如何更改动画样式?我可以使其淡入/淡出吗?

干杯!

Answers:


60

Marcus Zarra在SDK邮件列表中发布了一个很好的解决方案:

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:

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。

注意:如果您不在应用程序委托中,则[自身窗口]将不起作用。使用self.view.window,这要感谢user412500的帖子指出了这一点。


5
这是旧的!请参阅下面的Simo Salminen答案!MrDatabase,您应该切换此设置
Adam Waite

194

对于iPhone 3.0+,基本的淡入淡出最容易做到如下:

modalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[[self navigationController] presentModalViewController:modalViewController
                                               animated:YES];

2
完美的作品!!!我花了几秒钟的时间才意识到必须将过渡应用于呈现的viewController而不是self。
looneydoodle

您可以在Interface Builder中执行此操作,例如,添加模式搜索并将过渡设置Cross Dissolve为。
paulvs,2015年

8

要在iOS 4中更新Alpha褪色:

modalController.view.alpha = 0.0;
[self.view.window.rootViewController presentModalViewController:modalController animated:NO];
[UIView animateWithDuration:0.5
                 animations:^{modalController.view.alpha = 1.0;}];

2

应该是[self.view.window]为了使代码起作用

(至少这是它在iOS 3.2中的方式)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.