2011-06-24 79 views
1

我試圖更改旋轉視圖,因爲我的視圖必須明顯不同於縱向與橫向。現在我正在使用的代碼工作一次,然後應用程序在嘗試旋轉時凍結。無論哪個方向都沒有什麼不同。例如:如果我在風景和旋轉到肖像一切都很好,直到我旋轉回風景然後它凍結,並沒有什麼。在iPad上UIInterfaceOrientation期間更改UIViews

這裏是我使用來實現這一

在我「的viewDidLoad」方法的代碼

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];  
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(didRotate:) 
              name:UIDeviceOrientationDidChangeNotification 
              object:nil];  

然後我把這個用於旋轉:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return YES; 
} 

- (void)didRotate:(NSNotification *)notification 
{   
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    if ((orientation == UIDeviceOrientationLandscapeLeft) || 
     (orientation == UIDeviceOrientationLandscapeLeft)) 
    { 
     // present the other viewController, it's only viewable in landscape 
     [self.view addSubview:landScapeView]; 
    } 

    if ((orientation == UIDeviceOrientationLandscapeRight) || 
     (orientation == UIDeviceOrientationLandscapeRight)) 
    { 
     // present the other viewController, it's only viewable in landscape 
     [self.view addSubview:landScapeView]; 
    } 
    else if ((orientation == UIDeviceOrientationPortrait || 
      (orientation == UIDeviceOrientationPortrait)) 
    { 
     // get rid of the landscape controller 
     [self.view addSubview:portrait]; 
    } 
    else if ((orientation == UIDeviceOrientationPortraitUpsideDown || 
      (orientation == UIDeviceOrientationPortraitUpsideDown)) 
    { 
     // get rid of the landscape controller 
     [self.view addSubview:portrait]; 
    } 
} 
+0

請在下次提問時花時間格式化您的代碼,因爲這樣可以更容易閱讀,因此您可以得到更好的答案。另外,我不確定爲什麼你要在每個'if/else'條件中檢查'orientation'變量兩次以獲得相同的值;你應該簡化這個代碼。 –

+0

對不起,我通常......遲到了。感謝您修復它! – FreeAppl3

回答

2

你加入您的在旋轉方向特定的視圖,但永遠不會刪除所需的另一個。

// get rid of the landscape controller 
    ((orientation == UIDeviceOrientationPortraitUpsideDown || orientation == 
UIDeviceOrientationPortraitUpsideDown)) { 

[landScapeView removeFromSuperview];  
[self.view addSubview:portrait]; 
    } 

與其他方向類似。

請記住,從超級視圖版本中刪除,因此您需要有兩個視圖保留在外部。

+0

我很欣賞這種迴應。有道理,我嘗試了這一點,但我有一些事情需要解決。你絕對讓我走向正確的方向!謝謝! – FreeAppl3