一、在應(yīng)用中從豎屏模式強(qiáng)制轉(zhuǎn)換為橫屏模式
-
第一種方法:通過(guò)模態(tài)彈出視圖的方式纷宇,使得特定ViewController堅(jiān)持特定的interfaceOrientation
(1)iOS6之后提供了這樣一個(gè)方法默色,可以讓你的Controller倔強(qiáng)的堅(jiān)持某個(gè)特定的interfaceOrientation:
-
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0)
{
return UIInterfaceOrientationLandscapeRight;//左下右上顯示這里有5種供選擇哮笆,具體什么方向顯示菇曲,自己可以試一下
//UIInterfaceOrientationUnknown
//UIInterfaceOrientationPortrait
//UIInterfaceOrientationPortraitUpsideDown
//UIInterfaceOrientationLandscapeLeft
//UIInterfaceOrientationLandscapeRight
}
-
(2)當(dāng)然沉馆,使用這個(gè)方法是有前提的码党,就是當(dāng)前ViewController是通過(guò)全屏的Presentation(模態(tài)視圖)方式展現(xiàn)出來(lái)的。而且需要設(shè)置下面方法返回值為NO斥黑,這樣控制器就不會(huì)再進(jìn)行旋轉(zhuǎn)闽瓢,而是以你設(shè)定方向顯示。
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0)
{
return NO;
}
-
第二種方法:通過(guò)人為的辦法改變view.transform的屬性心赶。
具體辦法:view.transform一般是View的旋轉(zhuǎn)扣讼,拉伸移動(dòng)等屬性,類似view.layer.transform缨叫,區(qū)別在于View.transform是二維的椭符,也就是使用仿射的辦法通常就是帶有前綴CGAffineTransform的類(可以到API文檔里面搜索這個(gè)前綴的所有類),而view.layer.transform可以在3D模式下面的變化耻姥,通常使用的都是前綴為CATransform3D的類销钝。
這里要記住一點(diǎn),當(dāng)你改變過(guò)一個(gè)view.transform屬性或者view.layer.transform的時(shí)候需要恢復(fù)默認(rèn)狀態(tài)的話琐簇,記得先把他們重置可以使用view.transform = CGAffineTransformIdentity蒸健,或者view.layer.transform = CATransform3DIdentity座享,假設(shè)你一直不斷的改變一個(gè)view.transform的屬性,而每次改變之前沒有重置的話似忧,你會(huì)發(fā)現(xiàn)后來(lái)的改變和你想要的發(fā)生變化了渣叛,不是你真正想要的結(jié)果。
好了盯捌,上面介紹了旋轉(zhuǎn)的屬性淳衙,接下來(lái)就是關(guān)鍵了。官方提供了一個(gè)辦法就是查看當(dāng)前電池條的狀態(tài)UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;通過(guò)這個(gè)辦法饺著,你可以知道當(dāng)前屏幕的電池條的顯示方向箫攀,而且你還可以強(qiáng)制設(shè)置他的顯示方向,通過(guò)設(shè)置這個(gè)屬性就OK了幼衰,可以選擇是否動(dòng)畫改變電池條方向靴跛。有了這兩個(gè)那我們就可以任意的改變我們想要的顯示方式了。
(1)獲取當(dāng)前電池條的方向UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation (2)獲取當(dāng)前屏幕的大小CGRect frame = [UIScreen mainScreen].applicationFrame; (3)設(shè)置我們的View的中心點(diǎn) CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2)); (4)根據(jù)當(dāng)前電池條的方向渡嚣,獲取需要旋轉(zhuǎn)的角度的大小汤求。通常 - (CGAffineTransform)getARotation { if (orientation == UIInterfaceOrientationLandscapeLeft) { return CGAffineTransformMakeRotation(M_PI*1.5); } else if (orientation == UIInterfaceOrientationLandscapeRight) { return CGAffineTransformMakeRotation(M_PI/2); } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { return CGAffineTransformMakeRotation(-M_PI); } else { return CGAffineTransformIdentity; } } (5)可以動(dòng)畫的改變我們view的顯示方式了 [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:YES]; CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;(獲取當(dāng)前電池條動(dòng)畫改變的時(shí)間) [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:duration]; //在這里設(shè)置view.transform需要匹配的旋轉(zhuǎn)角度的大小就可以了。 //把(4)中返回的rotation賦給self.view.transform self.view.transform = [self getARotation]; [UIView commitAnimations];
-
第三種方法:通過(guò)setOrientation:的辦法強(qiáng)制性的旋轉(zhuǎn)到一個(gè)特定的方向严拒。
注意:Apple在3.0以后都不支持這個(gè)辦法了扬绪,這個(gè)辦法已經(jīng)成為了私有的了,但是要跳過(guò)App Stroe的審核裤唠,需要一點(diǎn)巧妙的辦法挤牛。
不要直接調(diào)用[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight]這樣的辦法來(lái)強(qiáng)制性的橫屏,這樣導(dǎo)致你的程序是很難通過(guò)App Store審核的种蘸。但是你可以選擇使用performSelector的辦法來(lái)調(diào)用它墓赴。具體就幾行代碼如下: //強(qiáng)制橫屏 if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationLandscapeRight]; }
總結(jié):如果第一種辦法可以滿足你需要的話,最好使用第一種辦法航瞭,因?yàn)榈谝环N方法在App Store肯定沒有問題诫硕,而且也比較簡(jiǎn)單;第二種方法在App Store也是沒問題的刊侯,但是稍微復(fù)雜一些章办;第三種的話是需要冒風(fēng)險(xiǎn)的,但是如果你的結(jié)構(gòu)太復(fù)雜了滨彻,導(dǎo)致使用前兩種辦法人為很難控制的話藕届,可以嘗試簡(jiǎn)單的使用第三種辦法。
二亭饵、屏幕自適應(yīng)重力感應(yīng)進(jìn)行旋轉(zhuǎn)休偶,實(shí)現(xiàn)對(duì)每個(gè)viewController的單獨(dú)控制:
-
子類化UINavigationController,增加方法
- (BOOL)shouldAutorotate { return self.topViewController.shouldAutorotate; } - (NSUInteger)supportedInterfaceOrientations { return self.topViewController.supportedInterfaceOrientations; }
-
并且設(shè)定其為程序入口辜羊,或指定為 self.window.rootViewController
隨后添加自己的view controller踏兜,如果想禁止某個(gè)view controller的旋屏:(支持全部版本的控制)- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(BOOL)shouldAutorotate { return NO; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
-
如果想又開啟某個(gè)view controller的全部方向旋屏支持:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAllButUpsideDown; } -(BOOL)shouldAutorotate { return YES; }
從而實(shí)現(xiàn)了對(duì)每個(gè)view controller的單獨(dú)控制词顾。
-
順便提一下,如果整個(gè)應(yīng)用所有view controller都不支持旋屏碱妆,那么干脆:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortrait; }