代码之家  ›  专栏  ›  技术社区  ›  Massimo Cafaro

旋转iPhone并实例化新的UIViewController?

  •  7
  • Massimo Cafaro  · 技术社区  · 17 年前

    我尝试在方法willRotateToInterfaceOrientation和didRotateFromInterfaceOrientation中实例化新的控制器,但是没有一个方法被调用!。我怀疑这是因为当前控制器是由导航控制器推入的,导航控制器本身由tabBarController处理。有线索吗?一个简单的代码片段将不胜感激。

    4 回复  |  直到 17 年前
        1
  •  29
  •   Matteo Alessani Cord LaBarre    15 年前

    更干净的方法是不使用UIInterfaceOrientation进行旋转。

    为了弥补这一点,我简单地订阅了 通知。微妙的不同。视图1不必支持自动旋转才能使此方法工作。在viewDidLoad中输入以下代码:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    

    然后只需定义方法检测:

    -(void) detectOrientation {
    
         if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
                ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
    
            //load view 2
    
    
        } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) {
    
            // load view 1
    
        }   
    }
    

    -(void) transformView2ToLandscape {
    
      NSInteger rotationDirection;
      UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
    
      if(currentOrientation == UIDeviceOrientationLandscapeLeft){
        rotationDirection = 1;
      }else {
        rotationDirection = -1;
      }
    
      CGRect myFrame = CGRectMake(0, 0, 480, 300);
      CGAffineTransform transform = [[self view2] transform];
      transform = CGAffineTransformRotate(transform, degreesToRadians(rotationDirection * 90));
      [[self view2] setFrame: myFrame];
      CGPoint center = CGPointMake(myFrame.size.height/2.0, myFrame.size.width/2.0);
      [[self view2] setTransform: transform];
      [[self view2] setCenter: center];
    
    }
    

    这就是我在视图中不支持自动旋转的情况下交换视图和旋转的方式。

        2
  •  7
  •   paulthenerd    17 年前

    因此,您需要通过以下方式强制它们允许所有方向更改: -(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation{ }

    然后,为了加载新的视图控制器,您应该响应:

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
      if((fromInterfaceOrientation == UIInterfaceOrientationPortrait) ||
         (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
      {    
        // Load the view controller you want to display in landscape mode...
      }
    }
    

    也可以使用:

    -(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    

    在我的例子中,我使用didRotateFromInterfaceOrientation来检查最后一个方向是否为纵向,如果是,则加载我希望以横向模式显示的视图。

    然后,我在正在加载的viewcontroller中实现了相同的方法,它负责检测方向何时变回纵向,并从视图堆栈中移除自身。

    希望对你有点帮助。

        3
  •  3
  •   Kornel    17 年前

    将其添加到选项卡栏控制器:

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
        [self.selectedViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    }
    
        4
  •  1
  •   Eric Petroelje    17 年前

    您是否实现了shouldAutorotateToInterfaceOrientation?如果没有,那可能就是你没有收到这些信息的原因。