在iPad上的UIInterfaceOrientation期间更改UIViews

时间:2011-06-24 07:10:24

标签: ios cocoa-touch uiview uiinterfaceorientation

我正在尝试更改轮换视图,因为我的视图必须在纵向与横向之间存在显着差异。现在我正在使用的代码工作一次然后应用程序在尝试旋转时冻结。任何一个方向都没有区别。例如:如果我在风景中并且旋转到肖像,一切都很有效,直到我旋转回到风景然后它冻结并且什么也不做。

以下是我用来实现此目的的代码

在我的“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];
    }  
}

1 个答案:

答案 0 :(得分:2)

您正在旋转时添加特定方向的视图,但从不删除另一个视图。

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

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

在另一个方向上类似。

请记住,从superview版本中删除,因此您需要在外部保留两个视图。