屏幕旋转控制分动态旋转和手动代码控制旋转,多见于视频播放。
一 .APP设置
-
在配置中关闭屏幕方向。
info.plist -
在
AppDelegate
添加如下方法 返回所需要的旋转方向。代码设置的方向优先级将大于info.plist
中的设置。
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-
UITabBarController
方向由选中的controller
的方向控制,在UITabBarController
中添加如下设置:
- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];;
}
- 在
UINavigationController
中由topViewController
方向决定
- (BOOL)shouldAutorotate {
return [self.topViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
二. 旋转控制
- 在需要动态控制旋转的
controller
配置:
//是否支持自动转屏
- (BOOL)shouldAutorotate
{
return YES;
}
// 支持哪些屏幕方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
// 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- 手动控制旋转
/// 旋转
/// @param aUIDeviceOrientation 需要设置的旋转方向
-(void)changeWithDeviceOrientation:(UIDeviceOrientation)aUIDeviceOrientation{
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
if (aUIDeviceOrientation != deviceOrientation) {
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] setValue:@(aUIDeviceOrientation) forKey:@"orientation"];
}
}
}
网友评论