如何在两个动画之间添加延迟?

6
我正在关闭一个模态视图控制器,然后立即呈现另一个模态视图控制器,但是我目前只能在第二个模态视图上使用动画,而不能在两个模态视图上同时使用。

有没有办法延迟过程,以便用户体验到两个动画?

以下代码当前有效,但用户显然只看到第二个动画:

// First one configure
detailViewController.modalPresentationStyle = UIModalPresentationFullScreen;
detailViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl; 
[self presentModalViewController:detailViewController animated:YES];

//Dismiss first one
[self dismissModalViewControllerAnimated:NO]; 

//Immediately configure and show second one
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navController animated:YES];

你尝试过使用 [self dismissModalViewControllerAnimated:YES]; 但没有成功吗? - Michael Dautermann
[UIView transitionWithView:mysuperview duration:0.75 options:UIViewAnimationTransitionFlipFromRight animations:^{ [self dismissModalViewControllerAnimated:NO]; } completion:nil] [UIView transitionWithView:mysuperview duration:0.75 options:UIViewAnimationTransitionFlipFromRight animations:^{ [self dismissModalViewControllerAnimated:NO]; } completion:nil] - Paresh Navadiya
4个回答

9
现在在 present modal view controller 中有一个完成块可用。请参见此链接:LINK。它适用于iOS5.0及以上版本。

这样做的好处是,如果您使用计时器方案,则不需要估计定时器延迟。

只需将第二个动画的代码放入块中即可:

//Block safe reference to self to prevent retain cycles
__block typeof (self) selfReference = self;

// First one configure
detailViewController.modalPresentationStyle = UIModalPresentationFullScreen;
detailViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl; 
[self presentModalViewController:detailViewController animated:YES completion:
^{
    //Dismiss first one
    [selfReference dismissModalViewControllerAnimated:NO]; 

    //Immediately configure and show second one
    navController.modalPresentationStyle = UIModalPresentationFormSheet;
    navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [selfReference presentModalViewController:navController animated:YES];
 }];

2
这是正确的解决方案。尝试使用计时器猜测第一个视图控制器何时完成动画是一种hack方法。 - jnic
太棒了!这两个动画一起看起来很棒,谢谢伙计,而且第一次使用块 - 太方便了! - TheLearner
2
没问题。我忘记在块中担心保留循环了,看一下编辑,我已经用适合块的selfReference替换了self。 - James Webster

1
创建一个选择器,它可以执行以下操作:
- (void)showSecondModalVC {
  //Dismiss first one
  [self dismissModalViewControllerAnimated:NO]; 

  //Immediately configure and show second one
  navController.modalPresentationStyle = UIModalPresentationFormSheet;
  navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
  [self presentModalViewController:navController animated:YES];
}

然后在主要的代码块中:

// First one configure
detailViewController.modalPresentationStyle = UIModalPresentationFullScreen;
detailViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl; 
[self presentModalViewController:detailViewController animated:YES];

[self performSelector:@selector(showSecondModalVC) 
         withObject:nil 
         afterDelay:0.5f];

您需要仔细观察第一个模态框显示所需的时间,以确保动画效果良好。


1

你可以用其他风格来做。

detailViewController.modalPresentationStyle = UIModalPresentationFullScreen;
detailViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl; 
[self presentModalViewController:detailViewController animated:YES];

[self dismissModalViewControllerAnimated:NO]; 

[self performSelector:@selector(someFunction) withObject:nil afterDelay:1.0];

- (void) someFunction{
//Immediately configure and show second one
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navController animated:YES];

}


1

请尝试使用这个:

// First one configure
detailViewController.modalPresentationStyle = UIModalPresentationFullScreen;
detailViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl; 
[self presentModalViewController:detailViewController animated:YES];

//Dismiss first one
[self dismissModalViewControllerAnimated:NO]; 
NSTimer Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(openSecondView) userInfo:nil repeats:NO];


-(void)openSecondView
{
    //Immediately configure and show second one
    navController.modalPresentationStyle = UIModalPresentationFormSheet;
    navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:navController animated:YES];
}

愉快的编码...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接