屏幕旋轉(zhuǎn)
推薦文檔
- 了解UIWindow——UIWindow實(shí)踐
- iOS屏幕旋轉(zhuǎn)問(wèn)題總結(jié)
- IOS:屏幕旋轉(zhuǎn)與變換
- [轉(zhuǎn)換屬性詳解](http : //www.cnblogs.com/iCocos/p /4639162.html)
- IOS 設(shè)備旋轉(zhuǎn)的內(nèi)部處理流程以及一些【優(yōu)化建議】
- [iOS 的全局禁止橫屏,但UIWebView的全屏播放視頻支,橫屏](http : //www.cnblogs.com/piaojin/p/5089127.html)
有時(shí)候我們需要判斷應(yīng)用程序當(dāng)前的方向,可以通過(guò)獲取設(shè)備當(dāng)前的方向來(lái)確定,從下面的定義你可以看到
<strong>UIInterfaceOrientation</strong>的定義是通過(guò)<strong>UIDeviceOrientation</strong>來(lái)完成的,有兩個(gè)概念:
- UIDeviceOrientation:硬件設(shè)備的方向(設(shè)備的物理方向)
- UIInterfaceOrientation:應(yīng)用程序界面的方向(界面顯示的方向)
- UIInterfaceOrientationMask:應(yīng)用程序屏幕支持的旋轉(zhuǎn)方向
<strong>UIDeviceOrientation:</strong>
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation))
NSLog(@"The orientation is landscape");
else if(UIDeviceOrientationIsPortrait(deviceOrientation))
NSLog(@"The orientation is portrait");
<strong> UIInterfaceOrientation:</strong>
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation== UIInterfaceOrientationLandscapeLeft ||orientation== UIInterfaceOrientationLandscapeRight) {
NSLog(@"The orientation is landscape");
}
<strong>UIInterfaceOrientationMask:</strong>
<!--獲取支持旋轉(zhuǎn)方向-->
UIInterfaceOrientationMask orientationMask =[[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:[UIApplication sharedApplication].keyWindow];
<!--設(shè)備是ipad-->
if ([UIDevice currentDevice].userInterfaceIdiom ==UIUserInterfaceIdiomPad) {
<!--支持全尺寸旋轉(zhuǎn)-->
if (orientationMask != UIInterfaceOrientationMaskAll) {
if (orientation== UIInterfaceOrientationLandscapeLeft ||orientation== UIInterfaceOrientationLandscapeRight) {
windowframe =CGRectMake(frame.origin.y, frame.origin.x, frame.size.height, frame.size.width);
}
}
}
1. 方案一
設(shè)置不可旋轉(zhuǎn)
設(shè)置不支持屏幕旋轉(zhuǎn),并設(shè)置默認(rèn)方向?yàn)檎7聪?。設(shè)置后會(huì)發(fā)現(xiàn),凡是添加到根控制器的視圖控制器都不再支持左右橫屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;//只支持這一個(gè)方向(正常的方向)
}
/**重點(diǎn):
根控制器的位置:
1.如果在UIViewController上,則在UIViewController對(duì)應(yīng)的.m文件中加入三個(gè)函數(shù)即可。
2.如果在UITabBarController上,則在UITabBarController對(duì)應(yīng)的.m文件中加入三個(gè)函數(shù)即可。
3.如果在UINavigationController上,則在UINavigationController對(duì)應(yīng)的.m文件中加入三個(gè)函數(shù)即可。
(不按情況執(zhí)行,問(wèn)題無(wú)法解決)
*/
在需要橫屏的控制器執(zhí)行左右橫屏
- (BOOL)shouldAutorotate
{
return Yes;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
2.方案二
以導(dǎo)航控制器為跟控制器為例:
#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.方案三 強(qiáng)制橫豎屏
私有方法 無(wú)法通過(guò)APPStore審核
這是網(wǎng)絡(luò)上可以查到的一般解決方法,用在iPhone上的APP上效果顯著。不過(guò),如果你用iPad上運(yùn)行項(xiàng)目后會(huì)發(fā)現(xiàn),需求還沒(méi)有完成。在iPad上處于橫屏的狀態(tài)下運(yùn)行的應(yīng)用程序,會(huì)發(fā)現(xiàn)應(yīng)用里的視圖是橫屏顯示的。這時(shí),根控制器默認(rèn)是不支持屏幕旋轉(zhuǎn)的,所以整個(gè)APP一直處于橫屏狀態(tài)。當(dāng)然,實(shí)現(xiàn)的MPMoviePlayerViewController子類還是可以正常旋轉(zhuǎn)的。所以,我們只要將應(yīng)用在iPad上橫屏狀態(tài)下,也能以正常豎屏方向進(jìn)入應(yīng)用就OK了!
/**
*強(qiáng)制轉(zhuǎn)成豎屏:
*/
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];
}
/**
* 強(qiáng)制豎屏
*/
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. 方案四 通過(guò)判斷狀態(tài)來(lái)控制
- (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;
}
應(yīng)用在iPad的上運(yùn)行時(shí),所有視圖都只會(huì)支持豎屏方向,為了讓視頻播放的視圖能夠支持橫豎屏旋轉(zhuǎn),需要用到變換屬性來(lái)模擬系統(tǒng)的橫豎屏效果。
這里是建立在方案二的基礎(chǔ)上實(shí)現(xiàn)的
#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;
}