2012-08-17 49 views
7

我正在尝试使用UIViewController的transitionFromViewController:toViewController:duration方法,但使用自定义动画。使用transitionFromViewController的UIViewControllers之间的自定义动画:toViewController:持续时间

我有添加作为孩子到自定义容器的UIViewController以下两个视图控制器:

  1. firstController - 这是的UITabBarController的实例
  2. secondController - 这是的UIViewController
的子类

以下代码按预期工作:

[self transitionFromViewController:firstController 
        toViewController:secondController 
          duration:2           
          options:UIViewAnimationOptionTransitionFlipFromLeft 
         animations:^(void){} 
         completion:^(BOOL finished){}]; 

但是我想创建一个自定义动画,其中firstController向左滑动,并由secondController从右侧滑入,类似于UINavigationControllers推送和弹出方法的工作方式。在将options更改为UIViewAnimationOptionTransitionNone之后,我试图在animations块中实现自定义动画,但完全没有成功。 firstController立即换成secondController没有和动画。

我真的很感激任何帮助。

谢谢

回答

15

这其实很简单。出于某种原因,我认为secondController的观点将低于或低于firstController的观点。我只试图动画firstController的观点。这当然是错误的。一旦transitionFromViewController:toViewController:duration被称为secondController的观点被置于firstController的观点。下面的代码工作:

CGFloat width = self.view.frame.size.width; 
CGFloat height = self.view.frame.size.height; 

secondController.view.frame = CGRectMake(width, 0, width, height); 

[self transitionFromViewController:firstController 
    toViewController:secondController 
    duration:0.4 
    options:UIViewAnimationOptionTransitionNone 
    animations:^(void) { 
     firstController.view.frame = CGRectMake(0 - width, 0, width, height); 
     secondController.view.frame = CGRectMake(0, 0, width, height); 
    } 
    completion:^(BOOL finished){ 
     [secondController didMoveToParentViewController:self]; 
    } 
]; 
+6

你应该叫[secondController didMoveToParentViewController:自我]。在完成处理程序也:) – banDedo 2012-12-22 21:55:02

+1

@banDedo我更新了代码。感谢您指出了这一点。需要! – 2014-10-22 22:57:36

+0

banDedo&Shaun F.感谢您的建议和编辑。 – Simple99 2014-10-23 13:49:26