2011-03-08 39 views
3

爲了使用UISplitViewController,我在從一個視圖控制器導航到另一個視圖控制器時替換了我的窗口根控制器。UIViewController的動畫設置錯誤方向

爲了有一些很好的過渡,而這樣做,我使用的是變焦效果是這樣的:

MyOtherViewController *controller = [[MyOtherViewController alloc] initWithNibName:@"MyOtherView" bundle:nil]; 
UIWindow *window = ((MyAppDelegate *)[[UIApplication sharedApplication] delegate]).window; 

controller.view.frame = [window frame]; 
controller.view.transform = CGAffineTransformMakeScale(0.01,0.01); 
controller.view.alpha = 0; 

[window addSubview:controller.view]; 

[UIView animateWithDuration:0.2 animations:^{ 
    controller.view.transform = CGAffineTransformMakeScale(1,1); 
    controller.view.alpha = 1.0; 
} completion:^(BOOL finished) { 
    if (finished) { 
     [self.view removeFromSuperview]; 
     window.rootViewController = controller; 
    } 
}]; 

,這工作得很好,只是在做動畫,新視圖總是無論當前的設備方向如何,都可以在縱向模式下進行定向。當動畫完成時,視圖正確定向。

我錯過了什麼?

事情我已經嘗試:

  • 把我的新控制器視圖的一個UIWindow
  • 唯一的子視圖使我的新控制器的根視圖控制器動畫開始

A之前好奇的是,如果我在我的方法開始處的窗口上做了遞歸描述,窗口框架被定義爲具有768x1024(即,縱向)的尺寸,並且其內部的視圖爲748x1024,但是具有[ 0,-1,1,0,0,0](做這個mea旋轉或什麼?它應該不是身份轉換嗎?)

回答

2

我終於明白出了什麼問題。由於框架不是一個真實的屬性,而是一種基於視圖邊界和視圖變換的計算值,我需要在設置與當前視圖相同的變換之後設置框架,並且在再次設置變換之前設置動畫的初始狀態。此外,我需要設置的幀與當前視圖當前使用的幀相同,因爲它考慮了窗口方向(或者像Rob Napier指出的那樣缺少其方向)

因此,沒有更多的瞭解,這裏是工作代碼:

MyOtherViewController *controller = [[MyOtherViewController alloc] initWithNibName:@"MyOtherView" bundle:nil]; 
UIWindow *window = [[UIApplication sharedApplication] keyWindow]; 

CGAffineTransform t = self.view.transform; 
controller.view.transform = t; 
controller.view.frame = self.view.frame; 
controller.view.transform = CGAffineTransformScale(t,.01,.01);; 
[window addSubview:controller.view]; 

controller.view.alpha = 0; 

[UIView animateWithDuration:0.2 animations:^{ 
    controller.view.transform = t; 
    controller.view.alpha = 1.0; 
} completion:^(BOOL finished) { 
    if (finished) { 
     [self.view removeFromSuperview]; 
     window.rootViewController = controller; 
     [controller release]; 
    } 
}]; 
3

UIWindow不旋轉。它有一個旋轉的視圖(如你所見)。不過,在這種情況下,我認爲問題很可能是您的視圖已經在此處進行了轉換,您需要將它連接起來,而不是像在setTransform:調用中那樣替換它。

你不應該問窗口的應用程序委託,你應該從視圖中獲取窗口(self.view.window)。

如果您在任何時候將視圖附加到窗口本身,而不是將其放置在旋轉視圖中,則需要通過遍歷層次結構來了解要匹配的視圖的有效變換:

- (CGAffineTransform)effectiveTransform { 
    CGAffineTransform transform = [self transform]; 
    UIView *view = [self superview]; 
    while (view) { 
     transform = CGAffineTransformConcat(transform, [view transform]); 
     view = [view superview]; 
    } 
    return transform; 
} 
+0

是我有點使用與我當前視圖中使用相同的轉換。由於它們都是窗口的直接子視圖,我想必須應用相同的變換才能獲得相同的結果是合乎邏輯的。 不幸的是,既沒有應用你提到的構圖,也沒有應用窗口變換,也沒有應用視圖變換。 – 2011-03-09 09:16:00

+0

我剛剛發佈我的答案。由於它證實了我懷疑這個窗口沒有旋轉,而是應用了一個變換,所以我選擇了這個。 – 2011-03-09 10:34:23