ios 如何使用淡出动画关闭模态 VC?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8956981/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:26:19  来源:igfitidea点击:

How to dismiss a modal VC with fade out animation?

iosanimationmodalviewcontrollerfadeoutdismiss

提问by Gruntcakes

I am using the following code in my presenting VC to fade in the child modal VC, and this works fine:

我在演示 VC 中使用以下代码淡入子模式 VC,这工作正常:

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

However I can't get it to fade out, I have tried a few things, this is the latest I tried that doesn't work:

但是我无法让它淡出,我尝试了一些东西,这是我尝试过的最新方法,但不起作用:

- (IBAction)dismissAction:(id)sender
{
if ([[self parentViewController] respondsToSelector:@selector(dismissModalViewControllerAnimated:)])
{
    [[self parentViewController] dismissModalViewControllerAnimated:YES];
    self.parentViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    self.parentViewController.view.alpha = 0.0;
    [UIView animateWithDuration:0.5
                     animations:^{self.parentViewController.view.alpha  = 1.0;}];
} else 
{
    [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
    self.presentedViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    self.presentedViewController.view.alpha = 0.0;
    [UIView animateWithDuration:0.5
                     animations:^{
                         self.presentedViewController.view.alpha  = 1.0;}];
}

}

}

The modal view controller is faded out but immediately, not over a time period like it is when its displayed.

模态视图控制器会立即淡出,而不是像显示时那样在一段时间内淡出。

回答by NJones

This (original part) is not to take away from H2CO3's correct answer. UIModalTransitionStyleCrossDissolvedoes pretty-much exactly the effect you're looking for. You are just not setting the modalTransitionStyle until it's to late. Replace all of your code with these functions in there respective positions:

这(原始部分)不是要从 H2CO3 的正确答案中删除。UIModalTransitionStyleCrossDissolve几乎完全符合您正在寻找的效果。您只是在为时已晚之前才设置 modalTransitionStyle 。用这些函数在各自的位置替换所有代码:

-(void)show{
    self.infoViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:self.infoViewController animated:YES];
}
- (IBAction)dismissAction:(id)sender{
    [self dismissModalViewControllerAnimated:YES];
}

Edit in response to timing being an issue:Let's talk about the offending code. We'll concentrate on just the if true part, since it's essentially identical to the else.

针对时间问题进行编辑:让我们谈谈有问题的代码。我们将只关注 if true 部分,因为它本质上与 else 相同。

[[self parentViewController] dismissModalViewControllerAnimated:YES];
self.parentViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.parentViewController.view.alpha = 0.0;
[UIView animateWithDuration:0.5
                 animations:^{self.parentViewController.view.alpha  = 1.0;}];

If you're looking for a reciprocal animation this isn't it. In your original animation you set the next view's alpha to 0, then presented the next view controller, then set it's view's alpha to 1. So logically you need to dismiss the view controller after the animation; This is really easy using blocks. The code would look something like this:

如果您正在寻找互惠动画,这不是它。在您的原始动画中,您将下一个视图的 alpha 设置为 0,然后呈现下一个视图控制器,然后将其视图的 alpha 设置为 1。所以从逻辑上讲,您需要在动画结束后关闭视图控制器;使用块真的很容易。代码看起来像这样:

[UIView animateWithDuration:0.5 animations:^{
    self.view.alpha = 0;
} completion:^(BOOL b){
    [self.presentingViewController dismissModalViewControllerAnimated:NO];
    self.view.alpha = 1;
}];

This line of code animates the view's alpha to 0, then (upon completion) dismisses the presented view controller, and sets the view's alpha back to 1. This is a reciprocal animation.

这行代码将视图的 alpha 动画设置为 0,然后(完成后)关闭呈现的视图控制器,并将视图的 alpha 设置回 1。这是一个交互动画。

回答by Gruntcakes

In the docs of UIViewController, we can find:

在 UIViewController 的文档中,我们可以找到:

@property(nonatomic, assign) UIModalTransitionStyle modalTransitionStyle

Set this property to UIModalTransitionStyleCrossDissolveand it will dissolve properly :)

将此属性设置为UIModalTransitionStyleCrossDissolve,它将正确溶解:)

Hope that helps.

希望有帮助。

回答by Agat

I guess, this might be useful for those heroic guys, who still tries to use MPMoviePlayerViewControllerin full screen mode and with orientation, which differs from the app major one.

我想,这可能对那些仍然尝试在全屏模式和方向上使用MPMoviePlayerViewController 的英雄有用,这与应用程序主要不同。

I've spent literally a half of working day playing with presenting MPMoviePlayerViewControllermodally or not modally. But there is no luck with that at all, in sense of transition animation changing. (The modal mode needed for setting orientation which differs from the app major orientation).

我已经花了半个工作日的时间来以模态或非模态的方式呈现MPMoviePlayerViewController。但在过渡动画变化的意义上,这根本就没有运气。(设置方向所需的模态模式与应用主方向不同)。

I tried presentViewController or presentModalViewController, but the result is the same. No matter what type is the modalTransitionStyle property set, if I do [... dismissViewControllerAnimated:true ...] then default transition style (UIModalTransitionStyleCoverVertical) used anyway. I tested that in iOS 5.1 device and in iOS 6 Simulator.

我尝试过presentViewController 或presentModalViewController,但结果是一样的。无论 modalTransitionStyle 属性设置为什么类型,如果我执行 [...dismissViewControllerAnimated:true ...] ,则无论如何都会使用默认过渡样式 (UIModalTransitionStyleCoverVertical)。我在 iOS 5.1 设备和 iOS 6 Simulator 中进行了测试。

I haven't tried any other types of controllers... Considering that common controller has method dismissMoviePlayerViewControllerAnimated, I can assume, that this method is used anyway for dismissing a video controller, no matter how did it appear. (Entering transitions didn't work for me as well, except they did not process as CoverVertical (in case of modalTransitionStyle changing).

我还没有尝试过任何其他类型的控制器......考虑到通用控制器有方法dismissMoviePlayerViewControllerAnimated,我可以假设,无论如何这个方法用于关闭视频控制器,无论它是如何出现的。(输入过渡对我也不起作用,除了它们没有作为 CoverVertical 处理(在 modalTransitionStyle 改变的情况下)。

So, my solution was not use the transition animation at all. I am sure, Apple had some reasons for allowing only some definite animation for MovieViewController (I really hope so, and that was made not because of "laziness"), but if they wanted some nice experience user would get, they failed, as in my app that's even better when the video appears without any animation (which is worse than CrossDisolving for sure) but that's better than banal CoverVertical.

所以,我的解决方案根本不使用过渡动画。我敢肯定,Apple 有一些理由只允许 MovieViewController 使用一些明确的动画(我真的希望如此,这不是因为“懒惰”),但是如果他们想要用户会获得一些好的体验,他们就失败了,如我的应用程序在没有任何动画的情况下出现视频时甚至更好(这肯定比 CrossDisolving 更糟糕),但这比平庸的 CoverVertical 更好。

Looking on that from developer's point of view, it really sounds like they spend much more money for designers to paint nice icons for customers instead of more pleasant and effective work of developers. (

从开发人员的角度来看,这听起来真的像是他们花更多的钱让设计师为客户绘制漂亮的图标,而不是开发人员更愉快和有效的工作。(