2013-01-17 22 views
6

中定义检测UIViewController上的接口旋转我有一个UIViewController处理主视图上的几个UIImageViews。在底部是一个UIToolbar,有几个项目可以互动。即使没有在 - (NSUInteger)supportedInterfaceOrientations

现在,当我旋转设备,我不希望viewController旋转,但只是UIImageViews。换句话说,底部的工具栏位于左侧(或右侧),但imageViews会正确旋转。

所以,通过使用这些方法

- (BOOL)shouldAutoRotate { 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskPortrait; 
} 

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
// rotate the image views here 
} 

在设备上的任何转动将不会被执行相结合,因为只有一个接口取向是支持(UIInterfaceOrientationMaskPortrait)。但是当我添加另一个界面方向以支持supportedInterfaceOrientations-方法时,视图控制器也将旋转。

即使只支持一个方向,我如何检测视图控制器的旋转?或者还有另一种可能性,可以根据不断变化的设备方向旋转UIViews?

感谢您的帮助!

+0

找到了答案 - 当然 - 10秒后在这里:ht TP://stackoverflow.com/questions/14387735/can-i-observe-when-a-uiviewcontroller-changes-interfaceorientation – uruk

回答

8

尝试使用UIDevice实例来检测设备物理方向的更改。 要开始接收通知,您可以使用这样的事情(在viewWillAppear:方法为例):

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

    //No reason to ask NSNotification because it many cases `userInfo` equals to 
    //@{UIDeviceOrientationRotateAnimatedUserInfoKey = 1;} 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceDidRotate) name:@UIDeviceOrientationDidChangeNotification object:nil]; 
} 

对于取消注册接收设备旋转活动,用这个(在viewWillDisappear:为例):

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 

    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; 
} 

而且这是一个例子deviceDidRotate功能:

- (void)deviceDidRotate { 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    switch (orientation) { 
     case UIDeviceOrientationPortrait: 
     case UIDeviceOrientationPortraitUpsideDown: 
      // do something for portrait orientation 
      break; 
     case UIDeviceOrientationLandscapeLeft: 
     case UIDeviceOrientationLandscapeRight: 
      // do something for landscape orientation 
      break; 

     default: 
      break; 
    } 
}