手动旋转方法断开侧开关

目前,我正在为iPad的绘图应用程序工作。 我需要重新定位和旋转应用程序中的工具栏时,将其放在不同的方向,同时保持绘图区在同一个地方。

我在这里find了一个方法来做到这一点。 它使用NSNotificationCenter监视轮换更改。 这将调用一个自定义的didRotate:方法,它将根据UIDeviceOrientation旋转和重新定位我的工具栏。

这部分工作正常。 但是,只要iPad上的侧面开关locking方向,工具栏重新定位到该位置即将启动。

例如:如果我在横向左侧启动应用程序并将其旋转到纵向,则工具栏将重新定位到屏幕的底部。 但是,一旦我使用滑动开关,它就会移动到屏幕左侧的横向左侧。

我正在使用的方法都在下面。

- (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; } - (void)didRotate:(NSNotification *)notification { UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; UIInterfaceOrientation interfaceOrientation; bool orientationFound = YES; if (deviceOrientation == UIDeviceOrientationPortrait) { interfaceOrientation = UIInterfaceOrientationPortrait; } else if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown) { interfaceOrientation = UIInterfaceOrientationPortraitUpsideDown; } else if (deviceOrientation == UIDeviceOrientationLandscapeLeft) { interfaceOrientation = UIInterfaceOrientationLandscapeRight; } else if (deviceOrientation == UIDeviceOrientationLandscapeRight) { interfaceOrientation = UIInterfaceOrientationLandscapeLeft; } else { orientationFound = NO; } if (orientationFound) { [self.toolbar changeToOrientation:interfaceOrientation withDuration:.25]; [self.tutorialOverlay changeToOrientation:interfaceOrientation]; } } - (void)changeToOrientation:(UIInterfaceOrientation)orientation withDuration:(float)duration { float angle; CGPoint origin; if (orientation == UIInterfaceOrientationPortrait) { angle = portraitAngle; origin = self.portraitOrigin; } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { angle = portraitUpsideDownAngle; origin = self.portraitUpsideDownOrigin; } else if (orientation == UIInterfaceOrientationLandscapeLeft) { angle = landscapeLeftAngle; origin = self.landscapeLeftOrigin; } else if (orientation == UIInterfaceOrientationLandscapeRight) { angle = landscapeRightAngle; origin = self.landscapeRightOrigin; } [UIView animateWithDuration:duration animations:^{ self.transform = CGAffineTransformMakeRotation(angle); CGRect rect = self.frame; rect.origin = origin; self.frame = rect; }]; } 

我强烈build议不要使用这种基于通知的方法。 请注意,通知的名称是UIDeviceOrientationDidChangeNotification ; 设备方向与界面方向不一样,导致很多小问题。 (例如,设备方向包括UIDeviceOrientationFaceDown,它从不与接口方向关联。)

我build议让你的视图控制器自动旋转自己; 这将为你放置工具栏等。 然后可以重写- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration将绘图区域恢复为您要维护的方向。 (你可以在上面的changeToOrientation方法中使用类似的代码,但是对于绘图区域,你不需要创build你自己的animation块,而且angular度都将被否定,因为你正在撤销更改视图控制器。)

我刚才在这里回答了类似的问题。 基本上只允许界面旋转,但旋转视图,你不想旋转'回'在willRotateToInterfaceOrientation:duration: