總結(jié)iOS App開發(fā)中控制屏幕旋轉(zhuǎn)的幾種方式
在iOS6之前的版本中,通常使用 shouldAutorotateToInterfaceOrientation 來單獨控制某個UIViewController的方向,需要哪個viewController支持旋轉(zhuǎn),只需要重寫shouldAutorotateToInterfaceOrientation方法。
但是iOS 6里屏幕旋轉(zhuǎn)改變了很多,之前的 shouldAutorotateToInterfaceOrientation 被列為 DEPRECATED 方法,查看UIViewController.h文件也可以看到:
// Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);
程序?qū)⑹褂萌缦?個方法來代替:
- (BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
除了重寫這個2個方法,IOS6之后要旋轉(zhuǎn)還有一些需要注意的地方,下面會細述。另外還有一個硬性條件,需要在Info.plist文件里面添加程序支持的所有方向,可以通過以下2種方式添加
1.

2.

另外要兼容IOS6之前的系統(tǒng),要保留原來的 shouldAutorotateToInterfaceOrientation 方法,還有那些 willRotateToInterfaceOrientation 等方法。
自動旋轉(zhuǎn)設置:
控制某個viewController旋轉(zhuǎn)并不是像IOS5或者IOS4一樣在這個viewController里面重寫上面那2個方法,而是需要在這個viewController的rootViewController(根視圖控制器)里面重寫,怎么解釋呢?就是最前面的那個viewController,直接跟self.window接觸的那個controller,比如以下代碼:
UIViewController *viewCtrl = [[UIViewController alloc] init];
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
if ([window respondsToSelector:@selector(setRootViewController:)]) {
self.window.rootViewController = navCtrl;
} else {
[self.window addSubview:navCtrl.view];
}
如果需要設置viewCtrl的旋轉(zhuǎn),那么不能在UIViewController里面重寫shouldAutorotate和supportedInterfaceOrientations方法,而是需要在navCtrl里面設置,又因為UINavigationController是系統(tǒng)控件,所以這里需要新建一個UINavigationController的子navigationController的子類,然后在里面實現(xiàn)shouldAutorotate和supportedInterfaceOrientations方法,比如:
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (BOOL)shouldAutorotate{
return YES;
}
eg1:如果上面的例子是self.window.rootViewController = viewCtrl,而不是navCtrl,那么上面的那2個控制旋轉(zhuǎn)的方法就應該寫在UIViewController里面!
eg2:如果viewCtrl又pushViewController到viewCtrl2,需要設置viewCtrl2的旋轉(zhuǎn),怎么辦呢? 還是在navCtrl里面控制,因為viewCtrl和viewCtrl2的rootViewController都是navCtrl,一般的寫法都是
UIViewController *viewCtrl2 = [[UIVewController alloc] init];
[self.navigationController.navigationController pushViewController:viewCtrl2 animated:YES];
所以要控制一個UINavigationController push到的所有viewController的旋轉(zhuǎn),那么就得在navCtrl里面區(qū)分是哪個viewController,以便對他們一一控制!同樣如果rootViewController是UITabbarController,那么需要在子類化的UITabbarController里面重寫那2個方法,然后分別控制!
但是有時候我初始化UINavigationController的時候,并不知道所有我所有需要push到的viewController,那么這里有一個通用的方法,就是讓viewController自己來控制自己,首先在navCtrl里面的實現(xiàn)方法改為以下方式:
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
全部調(diào)用self.topViewController,就是返回當前呈現(xiàn)出來的viewController里面的設置,然后在viewCtrl、viewCtrl2等等這些viewController里面重寫shouldAutorotate和supportedInterfaceOrientations,以方便設置每個viewController的旋轉(zhuǎn)
eg3:如果viewCtrl 是 presentModalViewController 到 viewCtrl3,那么viewCtrl3的旋轉(zhuǎn)設置就不在navCtrl里面了!如果presentModalViewController的viewController是navController、tabbarController包裝過的viewCtrl3,那么就應在新包裝的navController、tabbarController里面設置,如果是直接presentModalViewController到viewCtrl3,那么就在viewCtrl3里面設置
手動旋轉(zhuǎn)
手動旋轉(zhuǎn)也有2種方式,一種是直接設置 UIDevice 的 orientation,但是這種方式不推薦,上傳appStore有被拒的風險:
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];
}
第二種是假旋轉(zhuǎn),并沒有改變 UIDevice 的 orientation,而是改變某個view的 transform,利用 CGAffineTransformMakeRotation 來達到目的,比如:
self.view.transform = CGAffineTransformMakeRotation(M_PI/2)
下面講解采用第二種方式的各版本手動旋轉(zhuǎn):
思想是首先設置 statusBarOrientation,然后再改變某個view的方向跟 statusBarOrientation 一致!
那既然是旋轉(zhuǎn),最少也得有2個方向,那么還是少不了上面說的那個硬性條件,先在plist里面設置好所有可能需要旋轉(zhuǎn)的方向。既然是手動旋轉(zhuǎn),那么就要關閉自動旋轉(zhuǎn):
- (BOOL)shouldAutorotate{
return NO;
}
手動觸發(fā)某個按鈕,調(diào)用方法,這個方法的實現(xiàn)如下:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
self.view.bounds = CGRectMake(0, 0, kScreenHeight, 320);
注意:
1. 只需要改變self.view.transform,那么self.view的所有subview都會跟著自動變;其次因為方向變了,所以self.view的大小需要重新設置,不要使用self.view.frame,而是用bounds。
2. 如果shouldAutorotate 返回YES的話,下面設置setStatusBarOrientation 是不管用的!setStatusBarOrientation只有在shouldAutorotate 返回NO的情況下才管用!
強制旋轉(zhuǎn)屏幕
最近接手了一個項目,正常情況下使用查看圖片是沒問題的。
用到了 MWPhotoBrowser 這個第三方圖片瀏覽庫。
不過發(fā)現(xiàn)了一個問題,就是設備橫屏modal這MWPhotoBrowser的時候,發(fā)生了圖片位置錯亂。
實在沒辦法,所以想到了一個餿主意。
就是modal的時候使用代碼把設備強制旋轉(zhuǎn)回去。
//UIDevice+WJ.h
@interface UIDevice (WJ)
/**
* 強制旋轉(zhuǎn)設備
* @param 旋轉(zhuǎn)方向
*/
+ (void)setOrientation:(UIInterfaceOrientation)orientation;
@end
//UIDevice+WJ.m
#import "UIDevice+WJ.h"
@implementation UIDevice (WJ)
//調(diào)用私有方法實現(xiàn)
+ (void)setOrientation:(UIInterfaceOrientation)orientation {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[self currentDevice]];
int val = orientation;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
@end
相關文章
IOS 中l(wèi)oadView,viewDidLoad,viewDidUnload詳解及使用
這篇文章主要介紹了IOS 中l(wèi)oadView,viewDidLoad,viewDidUnload詳解及使用的相關資料,需要的朋友可以參考下2017-02-02
iOS App中實現(xiàn)播放音效和音樂功能的簡單示例
這篇文章主要介紹了iOS App中實現(xiàn)播放音效和音樂功能的簡單示例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
iOS App中數(shù)據(jù)管理框架Core Data的基本數(shù)據(jù)操作教程
Core Data框架能夠為我們提供比操作SQL關系型數(shù)據(jù)庫更簡單的數(shù)據(jù)管理方式,而且內(nèi)置于Xcode中配合IDE操作十分方便,下面我們就來看一下iOS App中數(shù)據(jù)管理框架Core Data的基本數(shù)據(jù)操作教程2016-06-06
iOS實現(xiàn)卡片式滾動效果 iOS實現(xiàn)電影選片效果
這篇文章主要為大家詳細介紹了iOS實現(xiàn)卡片式滾動效果,實現(xiàn)電影選片效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02
iOS如何去掉導航欄(UINavigationBar)下方的橫線
本篇文章主要介紹了iOS如何去掉導航欄(UINavigationBar)下方的橫線,非常具有實用價值,需要的朋友可以參考下2017-05-05

