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

发布于 2024-12-28 01:40:27 字数 1287 浏览 1 评论 0原文

我在演示 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;}];

但是我无法让它淡出,我尝试了一些方法,这是我尝试过的最新方法,但没有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;}];
}

}

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二货你真萌 2025-01-04 01:40:27

这(原始部分)并不是要剥夺 H2CO3 的正确答案。 UIModalTransitionStyleCrossDissolve 几乎完全达到了您正在寻找的效果。您只是没有设置 modalTransitionStyle ,直到为时已晚。将所有代码替换为相应位置的这些函数:

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

针对时间问题进行编辑:让我们来讨论一下有问题的代码。我们将只关注 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;}];

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

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

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

This (original part) is not to take away from H2CO3's correct answer. UIModalTransitionStyleCrossDissolve does 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:

-(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.

[[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:

[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.

岛徒 2025-01-04 01:40:27

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

@property(nonatomic, assign) UIModalTransitionStyle modalTransitionStyle

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

希望有所帮助。

In the docs of UIViewController, we can find:

@property(nonatomic, assign) UIModalTransitionStyle modalTransitionStyle

Set this property to UIModalTransitionStyleCrossDissolve and it will dissolve properly :)

Hope that helps.

旧竹 2025-01-04 01:40:27

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

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

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

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

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

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

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

I've spent literally a half of working day playing with presenting MPMoviePlayerViewController modally 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).

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.

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).

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.

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. (

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文