OC屏幕旋转相关
Posted hualuoshuijia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OC屏幕旋转相关相关的知识,希望对你有一定的参考价值。
OC屏幕旋转分为两个部分来说,第一个是开启了Device Orientation,开启了的话,自己旋转,没开启需要自己手动处理。因为现在大多数都是用自动布局,这个一般用不到,最近在看AVFoundation相关的东西,需要用到这个,所以总结下
第一部分,开启了自动旋转:
- (1)注册屏幕旋转通知:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; //注册屏幕旋转通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
- (void)orientChange:(NSNotification *)notification UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; if(orientation == UIDeviceOrientationPortrait) NSLog(@"home在下方"); self.deviceOrientationBtn.selected = NO; else if(orientation == UIDeviceOrientationPortraitUpsideDown) NSLog(@"home在上方"); self.deviceOrientationBtn.selected = NO; else if(orientation == UIDeviceOrientationLandscapeLeft) NSLog(@"home在右方"); self.deviceOrientationBtn.selected = YES; else if(orientation == UIDeviceOrientationLandscapeRight) NSLog(@"home在左方"); self.deviceOrientationBtn.selected = YES;
如果开启了之后我想手动横屏或者竖屏的话,也可以使用代码强制实现:
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight]
or
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"]
如果想进入改ViewController就横屏或者竖屏,退出之后恢复的话,可以在
-(void)viewWillAppear:(BOOL)animated
and
-(void)viewWillDisappear:(BOOL)animated:
两个方法中实现
第二部分,没有开启屏幕旋转,需要自己手动做一个假的来代替
(1)横屏设置的方法:
- (void)fullScreenWithDirection:(UIInterfaceOrientation)direction if (direction == UIInterfaceOrientationLandscapeLeft) [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES]; [UIView animateWithDuration:0.25 animations:^ self.view.transform = CGAffineTransformMakeRotation(M_PI / 2); completion:^(BOOL finished) // [self setFullStatusBarHiddenType:_fullStatusBarHiddenType]; ]; else if (direction == UIInterfaceOrientationLandscapeRight) [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES]; [UIView animateWithDuration:0.25 animations:^ self.view.transform = CGAffineTransformMakeRotation(0); completion:^(BOOL finished) // [self setFullStatusBarHiddenType:_fullStatusBarHiddenType]; ]; self.view.frame = [[UIApplication sharedApplication]keyWindow].bounds; [self.view setNeedsLayout]; [self.view layoutIfNeeded];
(2)竖屏还原
[UIView animateWithDuration:0.25 animations:^ self.view.transform = CGAffineTransformMakeRotation(0); self.view.frame = [[UIApplication sharedApplication]keyWindow].bounds; [self.view setNeedsLayout]; [self.view layoutIfNeeded]; completion:^(BOOL finished) ];
以上是关于OC屏幕旋转相关的主要内容,如果未能解决你的问题,请参考以下文章