旋转UIViewController以抵消UIInterfaceOrientation中的更改

时间:2010-03-22 03:14:00

标签: iphone iphone-sdk-3.0 uiview uiviewcontroller interface-orientation

我一直在搜索这个问题,找不到任何可以帮助我的事。

我有一个UIViewController包含在另一个UIViewController中。当父UIViewController旋转时,比如从Portrait到LandscapeLeft,我想让它看起来好像孩子没有旋转。也就是说。无论父母的方向如何,我都希望孩子与天空的方向相同。如果它的肖像中有一个直立的UIButton,我希望按钮的右侧在UIInterfaceOrientationLandscapeLeft中“向上”。

这可能吗?目前,我正在做这样的事情:

-(void) rotate:(UIInterfaceOrientation)fromOrientation: toOr:(UIInterfaceOrientation)toOrientation
{
    if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationLandscapeRight))
       || ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationLandscapeLeft)))
    {

    }
    if(((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
       || ((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationPortrait)))
    {

    }
    if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationLandscapeLeft))
       || ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationLandscapeRight)))
    {

    }
    if(((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
       || ((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationPortrait)))
    {

    }
    if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
       || ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationPortrait)))
    {

    }
    if(((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationLandscapeRight))
       || ((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationLandscapeLeft)))
    {

    }   
}

这似乎是对代码的完全浪费。此外,我打算使用CGAffineTransform(就像这里引用的那样:http://www.crystalminds.nl/?p=1102)但是我很困惑我是否应该改变视图的尺寸以匹配旋转后的尺寸。

这里的大噩梦是你必须跟踪一个全球“方向”变量。如果不这样做,则会失去幻觉,并且ViewController会旋转为任何内容。

我真的可以使用一些帮助,谢谢!

2 个答案:

答案 0 :(得分:5)

您可以做的最好的事情是根据您的界面方向更改子视图帧的帧。你可以这样做:

 #pragma mark -
 #pragma mark InterfaceOrientationMethods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
        //self.view = portraitView;
        [self changeTheViewToPortrait:YES andDuration:duration];

    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
        //self.view = landscapeView;
        [self changeTheViewToPortrait:NO andDuration:duration];
    }
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];

    if(portrait){
        //change the view and subview frames for the portrait view
    }
    else{   
        //change the view and subview  frames for the landscape view
    }

    [UIView commitAnimations];
}

希望这有帮助。

答案 1 :(得分:0)

我意识到了......让我们说我们的项目有多层ViewController(如果你将其他视图控制器的子视图添加到视图控制器中)

willRotateToInterfaceOrientation:不会为ViewController的第二层调用duration方法......

所以我做的是,在我从最顶层初始化第二层视图控制器之后,当在最顶层调用willRotateToInterfaceOrientation:duration方法时,我将调用willRotateToInterfaceOrientation:第二层视图控制器的持续时间为井