【问题标题】:Rotate UIViewController to counteract changes in UIInterfaceOrientation旋转 UIViewController 以抵消 UIInterfaceOrientation 的变化
【发布时间】:2010-03-22 03:14:00
【问题描述】:

我在这方面搜索了很多,但找不到任何可以帮助我的东西。

我有一个 UIViewController 包含在另一个 UIViewController 中。当父 UIViewController 旋转时,比如从 Portrait 到 LandscapeLeft,我想让它看起来好像孩子没有旋转。也就是说。无论父母的方向如何,我都希望孩子对天空有相同的方向。如果它的 UIButton 在 Portrait 中是直立的,我希望按钮的右侧在 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 会旋转到任何位置。

我真的需要一些帮助,谢谢!

【问题讨论】:

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


    【解决方案1】:

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

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

    希望这会有所帮助。

    【讨论】:

    • 谢谢马杜普。当您建议“更改纵向视图的视图和子视图框架”时 - 这是我要做 CGAffineTransform 的地方吗?或者我应该有某种单独的方式来旋转 UIView/UIViewController?我需要所有元素的行为方式相同,只是方向不同。另外,注释掉的 self.view = PortraitView 是什么意思?我会从一开始就使用这些方法,但我不知道如何使用侧向 UITableView 或侧向 UIButton 或类似的东西。再次感谢!
    • @Peter Hajas:我不执行 CGAffine 转换,而是从 xib 获取纵向和横向视图的帧位置,并相应地更改子视图的帧。
    • 我正在尝试实现此方法,但每当我旋转模拟器设备时,都不会触发任何方法。你知道为什么吗?
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-02-19
      • 1970-01-01
      • 2011-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      相关资源
      最近更新 更多