iOS屏幕旋轉(zhuǎn)

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è)備的方向
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容