如何用淡出动画解雇模态VC?

Gru*_*kes 16 animation fadeout dismiss modalviewcontroller ios

我在演示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;}];
Run Code Online (Sandbox Code Playgroud)

但是我无法让它淡出,我尝试了一些东西,这是我尝试过的最新功能:

- (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;}];
}
Run Code Online (Sandbox Code Playgroud)

}

模态视图控制器淡出但立即消失,而不是像显示时那样.

NJo*_*nes 50

这(原始部分)不是要取消H2CO3的正确答案.UIModalTransitionStyleCrossDissolve确切地说,你正在寻找的效果.你只是设置modalTransitionStyle,直到它为时已晚.在相应的位置用这些函数替换所有代码:

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

编辑以响应时间问题: 让我们谈谈有问题的代码.我们将专注于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;}];
Run Code Online (Sandbox Code Playgroud)

如果你正在寻找一个倒数动画,那就不是了.在原始动画中,您将下一个视图的alpha设置为0,然后显示下一个视图控制器,然后将其视图的alpha设置为1.因此逻辑上您需要在动画后关闭视图控制器; 使用块非常简单.代码看起来像这样:

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

这行代码将视图的alpha设置为0,然后(完成时)解除显示的视图控制器,并将视图的alpha设置回1.这是一个倒数动画.

  • 看来在iOS8中你还需要在presentationViewController上添加self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve,所以解雇也是CrossDissolve (4认同)

小智 10

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

@property(nonatomic, assign) UIModalTransitionStyle modalTransitionStyle
Run Code Online (Sandbox Code Playgroud)

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

希望有所帮助.