我在我的演示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;}];
但是我不能让它淡出,我已经尝试了几件事情,这是最新的,我试过不行:
- (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;}]; }
}
模态视图控制器淡出,但立即,而不是在它显示的时间段.
解决方法
这(原始部分)不是要从H2CO3的正确答案中夺走. UIModalTransitionStyleCrossDissolve完全符合您要查找的效果.你直到迟到才会设置modalTransitionStyle.将这些功能的所有代码替换为各自的位置:
-(void)show{ self.infoViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController:self.infoViewController animated:YES]; } - (IBAction)dismissAction:(id)sender{ [self dismissModalViewControllerAnimated:YES]; }
编辑响应时间是一个问题:我们来讨论违规代码.我们将专注于真正的部分,因为它基本上与其他部分完全相同.
[[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.这是一个互动动画.