2012-08-17 48 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