2011-01-05 72 views
1

我有這段代碼,設置爲當手機旋轉到橫向時出現clockView,然後當它返回到縱向時使其返回到mainView。但是當它返回到肖像時,肖像視圖處於橫向模式。我該如何解決?這是我的代碼。如何停止mainView旋轉?

-(void)willAnimateRotationToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    UIInterfaceOrientation toOrientation = self.interfaceOrientation; 

    if(toOrientation == UIInterfaceOrientationPortrait) 
    { 
     self.view = mainView; 
    } 

    else if(toOrientation == UIInterfaceOrientationLandscapeLeft) 
    { 
     self.view = clockView; 
    } 

    else if (toOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     self.view = mainView; 
    } 

    else if(toOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     self.view = clockView; 
    } 
} 

回答

3

這可能是更好的有兩個MAINVIEW和clockView爲您的視圖控制器的子視圖:

// in viewDidLoad do [self.view addSubView:mainView]; [self.view addSubView:clockView]; clockView.hidden = YES; 

-(void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    if(UIInterfaceOrientationIsPortrait(toOrientation)) { 
     mainView.hidden = NO; 
     clockView.hidden = YES; 
    } 
    else { 
     mainView.hidden = YES; 
     clockView.hidden = NO; 
    } 
} 

這樣,boths視圖以正確的方式自動旋轉和您的問題就會消失。