UIInterfaceOrientation方向枚舉:
UIInterfaceOrientationPortrait //home健在下
UIInterfaceOrientationPortraitUpsideDown //home健在上
UIInterfaceOrientationLandscapeLeft //home健在左
UIInterfaceOrientationLandscapeRight //home健在右
旋轉(zhuǎn)屏幕時觸發(fā)的函數(shù):
//旋轉(zhuǎn)方向發(fā)生改變時
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
//視圖旋轉(zhuǎn)動畫前一半發(fā)生之前自動調(diào)用
-(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
//視圖旋轉(zhuǎn)動畫后一半發(fā)生之前自動調(diào)用
-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {
}
//視圖旋轉(zhuǎn)之前自動調(diào)用
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
//視圖旋轉(zhuǎn)完成之后自動調(diào)用
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
}
//視圖旋轉(zhuǎn)動畫前一半發(fā)生之后自動調(diào)用
-(void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
}
如果項(xiàng)目中用了navigationViewController, 那么就應(yīng)該新建一個uinavigationViewController的子類侍芝,然后在這個類里面寫上下面的代碼州叠,在使用的時候就用自定義的這個navCtr留量, 就是說需要在根視圖里面控制
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return toInterfaceOrientation != UIDeviceOrientationPortraitUpsideDown;
}
- (BOOL)shouldAutorotate {
if ([self.topViewController isKindOfClass:[AddMovieViewController class]]) { // 如果是這個 vc 則支持自動旋轉(zhuǎn)
return YES;
}
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
1.Window級別的控制
對于UIApplicationDelegate的這個方法聲明忆绰,大多數(shù)情況下application就是當(dāng)前的application错敢,而window通常也只有一個稚茅。所以基本上通過window對橫屏豎屏interfaceOrientation的控制相當(dāng)于全局的亚享。
//每次試圖切換的時候都會走的方法,用于控制設(shè)備的旋轉(zhuǎn)方向.
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (_isRotation) {
return UIInterfaceOrientationMaskLandscape;
}else {
return UIInterfaceOrientationMaskPortrait;
}
}
2.Controller層面的控制
//哪些頁面支持自動轉(zhuǎn)屏
- (BOOL)shouldAutorotate{
return YES;
}
// viewcontroller支持哪些轉(zhuǎn)屏方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
3.使得特定ViewController堅(jiān)持特定的interfaceOrientation.
當(dāng)然欺税,使用這個方法是有前提的晚凿,就是當(dāng)前ViewController是通過全屏的 Presentation方式展現(xiàn)出來的.
- (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è)備的方向