2011-03-25 114 views
0

我創建了一個UIViewController(基於How to switch views when rotating)在設備旋轉時在2個視圖之間切換。每個視圖對於特定的方向都是「專門的」。旋轉時移動的視圖

它使用UIDeviceOrientationDidChangeNotification通知切換視圖:

-(void) deviceDidRotate: (NSNotification *) aNotification{ 

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    NSLog(@"Device rotated to %d!", orientation); 

    if ((orientation == UIDeviceOrientationPortrait) || 
     (orientation == UIDeviceOrientationPortraitUpsideDown)) { 
     [self displayView:self.portraitViewController.view]; 
    }else if ((orientation == UIDeviceOrientationLandscapeLeft) || 
       (orientation == UIDeviceOrientationLandscapeRight)) { 
     [self displayView:self.landscapeViewController.view]; 
    } 

} 

和排序的工作。當我旋轉到橫向,然後回到縱向時,出現問題。當回到肖像子視圖沒有在正確的地方顯示出來,特別是在UIPickerView:

首次肖像enter image description here

旋轉爲橫向enter image description here

返回至人像enter image description here

如果我重複輪換過程,情況會變得更糟。我究竟做錯了什麼?

的源代碼是在這裏:提前http://dl.dropbox.com/u/3978473/forums/Rotator.zip

謝謝!

回答

0

好的,我發現了錯誤。這非常簡單和愚蠢:我混合了框架和界限。

在displayView:代碼中,我將子視圖的框架設置爲父視圖的框架,當它應該是父框架的界限時。

1

要解決偏移問題,請重新編寫displayView:方法,如下所示。 (void)displayView:(UIView *)aView self.view = aView;(void)displayView:(UIView *)aView {} {self.view = aView; }

但是旋轉很奇怪。你應該檢查那部分代碼。 使用的UIViewController旋轉方法

  • (無效)willRotateToInterfaceOrientation:時間:
  • (無效)didRotateFromInterfaceOrientation:

的不是 - (無效)deviceDidRotate:

簡單得多,你會避免那種奇怪的彈跳,而且你不需要通知。 在我上面指定的方法上閱讀蘋果文檔。

希望這會有所幫助。

+0

我試着改變displayView:代碼,正如你所建議的那樣,但它不起作用。不管怎麼說,還是要謝謝你。 – cfischer 2011-03-27 13:50:05