1

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

我有一个 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 会旋转成任何东西。

我真的可以在这方面使用一些帮助,谢谢!

4

2 回答 2

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];
}

希望这可以帮助。

于 2010-03-22T08:12:05.823 回答
0

我意识到了一些事情..假设我们的项目有多个 ViewController 层(就像您将其他视图控制器的子视图添加到您的视图控制器一样)

willRotateToInterfaceOrientation:第2层ViewController不会调用duration方法...

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

于 2010-08-25T03:27:55.760 回答