iOS屏幕横竖屏旋转相关

iOS8之后的屏幕旋转和iOS6,7有很大不同,项目中自己之前遇到过这样的需求,从A界面呈现B界面,如果A横屏则呈现出的B也为横屏,如果A竖屏则呈现出的B也为竖屏,实现代码如下:

//当前屏幕高度

define MainScreenHeight [UIScreen mainScreen].bounds.size.height

//当前屏幕宽度

define MainScreenWidth [UIScreen mainScreen].bounds.size.width

//webView是当前满屏的控件

  • (void)viewDidLoad {
    [super viewDidLoad]; [UIApplication sharedApplication].statusBarHidden = YES;
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsLandscape(orientation)) { if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) { webViewWidth = MainScreenWidth; webViewHeight = MainScreenHeight; } else { webViewWidth = MainScreenHeight; webViewHeight = MainScreenWidth; } } else { webViewWidth = MainScreenWidth; webViewHeight = MainScreenHeight; }}

//隐藏状态栏- (BOOL)prefersStatusBarHidden{ return YES;}//设置是否支持旋转- (BOOL)shouldAutorotate { UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsPortrait(orientation)) { // 如果状态栏竖着的,不支持controller的旋转 return NO; } else if (UIInterfaceOrientationIsLandscape(orientation)) { return YES; } return NO;}// 直接返回支持的旋转方向,该方法在iPad上的默认返回值是UIInterfaceOrientationMaskAll,iPhone上的默认返回值是UIInterfaceOrientationMaskAllButUpsideDown- (NSUInteger)supportedInterfaceOrientations { UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsLandscape(orientation)) { return UIInterfaceOrientationMaskLandscape; } return UIInterfaceOrientationMaskPortrait;}

使用VC旋转通过statusBarOrientation的方向判断旋转方向,在iOS8之前横屏后,屏幕的宽和高的数值是对换了(即宽变成了高,高变成了宽),但在iOS8之后宽高并没有对换,所以在iOS8之后旋转横屏如果不对换宽高值的话,就会造成屏幕一侧为空的情况.(代码是挺早之前写的了,可能会有些乱)上面的代码是从A界面呈现B界面,如果A横屏,则present横屏vc,竖屏下present竖屏vc。
如果仅在当前vc进行横竖屏旋转操作,可以监听系统的通知『UIWindowWillRotateNotification』,如下:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(screenRotate:animation:) name:@"UIWindowWillRotateNotification" object:nil];

  • (void)screenRotate:(NSNotification *)noti animation:(BOOL)animation{UIInterfaceOrientation orientation = [[noti.userInfo objectForKey:@"UIWindowNewOrientationUserInfoKey"] integerValue]; if (!noti) { return; } animation = YES;NSTimeInterval i = [UIApplication sharedApplication].statusBarOrientationAnimationDuration; NSTimeInterval time = 0.3 + i; if (!animation) { time = 0.0; } switch (orientation) {case UIInterfaceOrientationPortrait: { } break;case UIInterfaceOrientationPortraitUpsideDown: { } break;case UIInterfaceOrientationLandscapeRight: {// 这里是给相应的view对应旋转 [UIView animateWithDuration:time animations:^{_userVc.view.transform = CGAffineTransformMakeRotation(M_PI_2); } completion:nil]; } break;case UIInterfaceOrientationLandscapeLeft: { // 这里是给相应的的view对应旋转 [UIView animateWithDuration:time animations:^{_userVc.view.transform = CGAffineTransformMakeRotation(-M_PI_2); } completion:nil]; } break; default: break; }}

你可能感兴趣的:(iOS屏幕横竖屏旋转相关)