屏幕旋转
推荐文档
- 了解UIWindow——UIWindow实践
- iOS屏幕旋转问题总结
- IOS:屏幕旋转与变换
- [转换属性详解](http : //www.cnblogs.com/iCocos/p /4639162.html)
- IOS 设备旋转的内部处理流程以及一些【优化建议】
- [iOS 的全局禁止横屏,但UIWebView的全屏播放视频支,横屏](http : //www.cnblogs.com/piaojin/p/5089127.html)
有时候我们需要判断应用程序当前的方向,可以通过获取设备当前的方向来确定,从下面的定义你可以看到
UIInterfaceOrientation的定义是通过UIDeviceOrientation来完成的,有两个概念:
- UIDeviceOrientation:硬件设备的方向(设备的物理方向)
- UIInterfaceOrientation:应用程序界面的方向(界面显示的方向)
- UIInterfaceOrientationMask:应用程序屏幕支持的旋转方向
UIDeviceOrientation:
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation))
NSLog(@"The orientation is landscape");
else if(UIDeviceOrientationIsPortrait(deviceOrientation))
NSLog(@"The orientation is portrait");
UIInterfaceOrientation:
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation== UIInterfaceOrientationLandscapeLeft ||orientation== UIInterfaceOrientationLandscapeRight) {
NSLog(@"The orientation is landscape");
}
UIInterfaceOrientationMask:
UIInterfaceOrientationMask orientationMask =[[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:[UIApplication sharedApplication].keyWindow];
if ([UIDevice currentDevice].userInterfaceIdiom ==UIUserInterfaceIdiomPad) {
if (orientationMask != UIInterfaceOrientationMaskAll) {
if (orientation== UIInterfaceOrientationLandscapeLeft ||orientation== UIInterfaceOrientationLandscapeRight) {
windowframe =CGRectMake(frame.origin.y, frame.origin.x, frame.size.height, frame.size.width);
}
}
}
1. 方案一
设置不可旋转
设置不支持屏幕旋转,并设置默认方向为正常反向。设置后会发现,凡是添加到根控制器的视图控制器都不再支持左右横屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向)
}
/**重点:
根控制器的位置:
1.如果在UIViewController上,则在UIViewController对应的.m文件中加入三个函数即可。
2.如果在UITabBarController上,则在UITabBarController对应的.m文件中加入三个函数即可。
3.如果在UINavigationController上,则在UINavigationController对应的.m文件中加入三个函数即可。
(不按情况执行,问题无法解决)
*/
在需要横屏的控制器执行左右横屏
- (BOOL)shouldAutorotate
{
return Yes;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
2.方案二
以导航控制器为跟控制器为例:
#pragma 横竖屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
return YES;
}
return (toInterfaceOrientation == UIInterfaceOrientationMaskPortrait);
}
- (BOOL)shouldAutorotate
{
if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
return YES;
}
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
3.方案三 强制横竖屏
私有方法 无法通过APPStore审核
这是网络上可以查到的一般解决方法,用在iPhone上的APP上效果显着。不过,如果你用iPad上运行项目后会发现,需求还没有完成。在iPad上处于横屏的状态下运行的应用程序,会发现应用里的视图是横屏显示的。这时,根控制器默认是不支持屏幕旋转的,所以整个APP一直处于横屏状态。当然,实现的MPMoviePlayerViewController子类还是可以正常旋转的。所以,我们只要将应用在iPad上横屏状态下,也能以正常竖屏方向进入应用就OK了!
/**
*强制转成竖屏:
*/
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
/**
* 强制竖屏
*/
NSArray * selectorNameCharacterArray = @[@"s",@"e",@"t",@"O",@"r",@"i",@"e",@"n",@"t",@"a",@"t",@"i",@"o",@"n",@":"];
NSString * selectorName = [selectorNameCharacterArray componentsJoinedByString:@""];
SEL selector = NSSelectorFromString(selectorName);
if ([[UIDevice currentDevice] respondsToSelector:selector]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
4. 方案四 通过判断状态来控制
- (void)deviceOrientationDidChange: (NSNotification *)notification
{
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
CGFloat startRotation = [[self valueForKeyPath:@"layer.transform.rotation.z"] floatValue];
CGAffineTransform rotation;
switch (interfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 270.0 / 180.0);
break;
case UIInterfaceOrientationLandscapeRight:
rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 90.0 / 180.0);
break;
case UIInterfaceOrientationPortraitUpsideDown:
rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 180.0 / 180.0);
break;
default:
rotation = CGAffineTransformMakeRotation(-startRotation + 0.0);
break;
}
view.transform = rotation;
}
应用在iPad的上运行时,所有视图都只会支持竖屏方向,为了让视频播放的视图能够支持横竖屏旋转,需要用到变换属性来模拟系统的横竖屏效果。
这里是建立在方案二的基础上实现的
#pragma 横竖屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
return YES;
}
return (toInterfaceOrientation == UIInterfaceOrientationMaskPortrait);
}
- (BOOL)shouldAutorotate
{
if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
return YES;
}
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
if (iPad) {
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation)){
switch (deviceOrientation) {
case 3:
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.presentedViewController.view.bounds = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
self.presentedViewController.view.transform = CGAffineTransformMakeRotation(-M_PI*1.5);
[UIView commitAnimations];
[USER_DEFAULTS setValue:@(deviceOrientation) forKey:@"deviceOrientation"];
}
break;
case 4:
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.presentedViewController.view.bounds = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
self.presentedViewController.view.transform = CGAffineTransformMakeRotation(M_PI*1.5);
[UIView commitAnimations];
[USER_DEFAULTS setValue:@(deviceOrientation) forKey:@"deviceOrientation"];
}
break;
default:
break;
}
}else{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.presentedViewController.view.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.presentedViewController.view.transform = CGAffineTransformMakeRotation(0);
[USER_DEFAULTS setValue:@(deviceOrientation) forKey:@"deviceOrientation"];
[UIView commitAnimations];
}
}
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}