如何旋转控制器中不允许旋转其接口的视图?

帮我解决任务 – 我有一个viewController不允许旋转其接口:

- (BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

但是我需要旋转出现在这个控制器中的alertView! 所以如果用户旋转设备,alertView应该跟随旋转,主界面仍然静止。 我试图订阅通知:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

但在我的deviceRotated:我收到这样的有效载荷的通知: NSConcreteNotification 0x101dc50 {name = UIDeviceOrientationDidChangeNotification; object = <UIDevice: 0x103e640>; userInfo = { UIDeviceOrientationRotateAnimatedUserInfoKey = 1; }} NSConcreteNotification 0x101dc50 {name = UIDeviceOrientationDidChangeNotification; object = <UIDevice: 0x103e640>; userInfo = { UIDeviceOrientationRotateAnimatedUserInfoKey = 1; }}

什么是UIDeviceOrientationRotateAnimatedUserInfoKey ? 而我如何才能知道当前的interfaceOrientation? 或者build议获取当前方向并旋转alertView的更好方法。

我试图使用

(BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)toInterfaceOrientation

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration但是这些方法不会在iOS6上调用:(

另外,还有什么其他方法可以用来知道设备正在旋转到某个方向吗? 提前致谢! PS我知道这个任务似乎有点愚蠢,但这是客户的要求。 对不起:)

UIDevice中有一个orientationvariables,它包含当前的设备方向 。 你可以使用它而不是界面方向(不会像你已经注意到的那样旋转)。

首先你订阅设备方向的变化,一个好的地方是viewWillAppear和取消订阅viewWillDisappear

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } 

在这个例子中,我们表示我们要调用orientationChanged:当设备旋转时,让我们执行它:

 #pragma mark - Orientation change events - (void)orientationChanged:(NSNotification*)notification { UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; // Calculate rotation angle CGFloat angle; switch (deviceOrientation) { case UIDeviceOrientationPortraitUpsideDown: angle = M_PI; break; case UIDeviceOrientationLandscapeLeft: angle = M_PI_2; break; case UIDeviceOrientationLandscapeRight: angle = - M_PI_2; break; default: angle = 0; break; } // Apply rotation static NSTimeInterval animationDuration = 0.3; [UIView animateWithDuration:animationDuration animations:^{ _viewToRotate.transform = CGAffineTransformMakeRotation(angle); }]; } 

我删除了以前的答案,因为我误解了这个问题(实际上这是一个非常奇怪的请求,但…)。 我build议你使用无限旋转蒙板的容器视图控制器,然后将你的控制器添加为子项目:CVC根据以下方法将旋转事件转发给子项:

 - (BOOL) automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers { return NO; } 

所以它会得到所有的事件,并只向你想要的那个孩子转发。

最后Container将pipe理警报并正确旋转。

由于方向locking在特定的位置,所以参考状态栏不会有太大的帮助。 我的build议是使用加速度计,并根据收到的XYZ值相应地更改alertView。 您可以通过使用CGAffineTransformMakeRotation或CGAffineTransformRotate方法来旋转alertView。

使用加速度计的一个例子如下:

 - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{ accelerationValue[0] = acceleration.x; accelerationValue[1] = acceleration.y; accelerationValue[2] = acceleration.z; NSLog(@"Acceleration X-axis: %1.1f, Y-axis: %1.1f, Z-axis: %1.1f", acceleration.x, acceleration.y, acceleration.z); } 

当然,加速计需要在相应的ViewController中设置和创build,但是我认为你明白了我的观点。