2011-01-05 68 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视图以正确的方式自动旋转和您的问题就会消失。