iOS 知識小集(橫豎屏切換)
轉(zhuǎn)載自 http://www.cocoachina.com/ios/20160722/17148.html
iOS 中橫豎屏切換的功能,在開發(fā)iOS app中總能遇到翠订。以前看過幾次,感覺簡單序厉,但是沒有敲過代碼實現(xiàn)鳖孤,最近又碰到了,demo嘗試了幾種情況涌韩,這里就做下總結(jié)熏版。
注意
橫屏兩種情況是反的你知道嗎纷责?
UIInterfaceOrientationLandscapeRight與UIInterfaceOrientationMaskLandscapeRight都代表橫屏,Home鍵在右側(cè)的情況撼短;UIDeviceOrientationLandscapeLeft則是Home鍵在左側(cè)再膳。
一般情形
所有界面都支持橫豎屏切換
如果App的所有切面都要支持橫豎屏的切換,那只需要勾選【General】 中的【Device Orientation】曲横,選擇希望支持的方向即可喂柒。
blob.png
圖中支持豎屏和Home在右側(cè)
如上設(shè)置完之后,當(dāng)設(shè)備豎屏的時候禾嫉,所有的界面都是豎屏顯示的灾杰;而當(dāng)設(shè)備橫屏Home在右側(cè)時,所有的界面會橫屏顯示熙参。其他方向不支持艳吠,界面不會改變。
這里有個坑:
在iOS 9 之后橫屏?xí)r尊惰,狀態(tài)欄會消失讲竿。
解決方法:確保plist 中的【View controller-based status bar appearance】為YES,然后重寫ViewController的 - (BOOL)prefersStatusBarHidden 弄屡,返回值是NO。
- (BOOL)prefersStatusBarHidden
{
return NO;
}
特殊情形
個別界面固定方向鞋诗,其他所有界面都支持橫豎屏切換
這種情況膀捷,在【General】-->【Device Orientation】中設(shè)置好支持的方向后,只需要在這些特殊的視圖控制器中重寫兩個方法:
// 支持設(shè)備自動旋轉(zhuǎn)
- (BOOL)shouldAutorotate
{
return YES;
}
/**
*? 設(shè)置特殊的界面支持的方向,這里特殊界面只支持Home在右側(cè)的情況
*/
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
個別界面支持橫豎屏切換削彬,其他所有界面都固定方向
可能大多數(shù)App會是這種需求全庸,某些特殊界面只能橫屏秀仲,如視頻播放類App。
這里有兩種處理方式:
方式一
在【General】-->【Device Orientation】中設(shè)置好需要支持的所有方向壶笼。然后使用一個基類控制器神僵,在基類控制器中重寫兩個控制橫豎屏的方法:
// 支持設(shè)備自動旋轉(zhuǎn)
- (BOOL)shouldAutorotate
{
return YES;
}
// 支持豎屏顯示
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
再然后,特殊的界面上再重寫這倆方法覆劈,讓其可以自動切換方向保礼。
// 如果需要橫屏的時候,一定要重寫這個方法并返回NO
- (BOOL)prefersStatusBarHidden
{
return NO;
}
// 支持設(shè)備自動旋轉(zhuǎn)
- (BOOL)shouldAutorotate
{
return YES;
}
// 支持橫屏顯示
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
// 如果該界面需要支持橫豎屏切換
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
// 如果該界面僅支持橫屏
// return UIInterfaceOrientationMaskLandscapeRight责语;
}
方式二
用方式一的方法炮障,還需要借助一個基類,所有的控制器都要繼承這個基類坤候,太麻煩胁赢?
另一種方式,是借助通知來控制界面的橫豎屏切換白筹。
還是整個App中大部分界面都是豎屏智末,某個界面可以橫豎屏切換的情況。
首先徒河,在【General】-->【Device Orientation】設(shè)置僅支持豎屏系馆,like this:
blob.png
Device Orientation
然后在特殊的視圖控制器里的ViewDidLoad中注冊通知:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
通知方法的實現(xiàn)過程:
- (void)deviceOrientationDidChange
{
NSLog(@"deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);
if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
[self orientationChange:NO];
//注意: UIDeviceOrientationLandscapeLeft 與 UIInterfaceOrientationLandscapeRight
} else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
[self orientationChange:YES];
}
}
- (void)orientationChange:(BOOL)landscapeRight
{
if (landscapeRight) {
[UIView animateWithDuration:0.2f animations:^{
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}];
} else {
[UIView animateWithDuration:0.2f animations:^{
self.view.transform = CGAffineTransformMakeRotation(0);
self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}];
}
}
// 用到的兩個宏:
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
最重要的一點:
需要重寫如下方法,并且返回NO虚青。
- (BOOL)shouldAutorotate
{
return NO;
}
這樣它呀,在設(shè)備出于橫屏?xí)r,界面就會變成橫屏棒厘,設(shè)備處于豎屏?xí)r纵穿,界面就會變成豎屏。
填坑
上面方式二奢人,因為【General】-->【Device Orientation】因為只設(shè)置了豎屏谓媒,所以當(dāng)橫屏?xí)r,如果有鍵盤彈出何乎,鍵盤是豎屏?xí)r的樣式句惯。
解決辦法:在【General】-->【Device Orientation】中加上橫屏?xí)r的方向。
如果VieController 是放在UINavigationController或者UITabBarController中支救,需要重寫它們的方向控制方法抢野。
// UINavigationController:
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
// UITabBarController:
- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
如果想要點擊某個按鈕之后,強制將豎屏顯示的界面變成橫屏呢各墨?
有人可能會想到這樣寫:
// 橫屏
- (IBAction)landscapAction:(id)sender {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
[self orientationChange:YES];
}
但是按照上面的寫法指孤,會導(dǎo)致返回到之前的界面時,視圖方向錯誤,即使返回前執(zhí)行如下代碼:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
[self orientationChange:NO];
也沒有作用恃轩,下面是在開源工程中無意看到的寫法:
// 橫屏
- (IBAction)landscapAction:(id)sender {
[self interfaceOrientation:UIInterfaceOrientationLandscapeRight];
}
// 豎屏
- (IBAction)portraitAction:(id)sender {
[self interfaceOrientation:UIInterfaceOrientationPortrait];
}
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
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? ? ? ? ? ? ? ? ? = orientation;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
上面的方法會將設(shè)備的方向強制設(shè)置為某個方向结洼,然后再監(jiān)控設(shè)備方向改變的通知,即可實現(xiàn)橫豎屏切換叉跛。
這里有一個用JS 和原生item 控制橫豎屏切換的Demo松忍。地址
這是效果圖:
727768-9d329256ad34abb8.gif
橫豎屏切換.gif
橫豎屏切換總結(jié)就到這來了,Have Fun!