UIInterfaceOrientation方向枚舉:
UIInterfaceOrientationPortrait //home健在下
UIInterfaceOrientationPortraitUpsideDown //home健在上
UIInterfaceOrientationLandscapeLeft //home健在左
UIInterfaceOrientationLandscapeRight //home健在右
旋轉(zhuǎn)屏幕時(shí)觸發(fā)的函數(shù):
//旋轉(zhuǎn)方向發(fā)生改變時(shí)
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
//視圖旋轉(zhuǎn)動(dòng)畫(huà)前一半發(fā)生之前自動(dòng)調(diào)用
-(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
//視圖旋轉(zhuǎn)動(dòng)畫(huà)后一半發(fā)生之前自動(dòng)調(diào)用
-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {
}
//視圖旋轉(zhuǎn)之前自動(dòng)調(diào)用
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
//視圖旋轉(zhuǎn)完成之后自動(dòng)調(diào)用
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
}
//視圖旋轉(zhuǎn)動(dòng)畫(huà)前一半發(fā)生之后自動(dòng)調(diào)用
-(void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
}
如果項(xiàng)目中用了navigationViewController, 那么就應(yīng)該新建一個(gè)uinavigationViewController的子類,然后在這個(gè)類里面寫(xiě)上下面的代碼,在使用的時(shí)候就用自定義的這個(gè)navCtr, 就是說(shuō)需要在根視圖里面控制
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return toInterfaceOrientation != UIDeviceOrientationPortraitUpsideDown;
}
- (BOOL)shouldAutorotate {
if ([self.topViewController isKindOfClass:[AddMovieViewController class]]) { // 如果是這個(gè) vc 則支持自動(dòng)旋轉(zhuǎn)
return YES;
}
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
1.Window級(jí)別的控制
對(duì)于UIApplicationDelegate的這個(gè)方法聲明,大多數(shù)情況下application就是當(dāng)前的application,而window通常也只有一個(gè)。所以基本上通過(guò)window對(duì)橫屏豎屏interfaceOrientation的控制相當(dāng)于全局的。
//每次試圖切換的時(shí)候都會(huì)走的方法,用于控制設(shè)備的旋轉(zhuǎn)方向.
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (_isRotation) {
return UIInterfaceOrientationMaskLandscape;
}else {
return UIInterfaceOrientationMaskPortrait;
}
}
2.Controller層面的控制
//哪些頁(yè)面支持自動(dòng)轉(zhuǎn)屏
- (BOOL)shouldAutorotate{
return YES;
}
// viewcontroller支持哪些轉(zhuǎn)屏方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
3.使得特定ViewController堅(jiān)持特定的interfaceOrientation.
當(dāng)然,使用這個(gè)方法是有前提的,就是當(dāng)前ViewController是通過(guò)全屏的 Presentation方式展現(xiàn)出來(lái)的.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);
4.當(dāng)前屏幕方向interfaceOrientation的獲取.
有3種方式可以獲取到“當(dāng)前interfaceOrientation”:
controller.interfaceOrientation,獲取特定controller的方向
[[UIApplication sharedApplication] statusBarOrientation] 獲取狀態(tài)條相關(guān)的方向
[[UIDevice currentDevice] orientation] 獲取當(dāng)前設(shè)備的方向