前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS屏幕旋转

iOS屏幕旋转

作者头像
且行且珍惜_iOS
发布2018-05-22 16:54:06
1.6K0
发布2018-05-22 16:54:06
举报

UIInterfaceOrientation方向枚举: UIInterfaceOrientationPortrait //home健在下 UIInterfaceOrientationPortraitUpsideDown //home健在上 UIInterfaceOrientationLandscapeLeft //home健在左 UIInterfaceOrientationLandscapeRight //home健在右

代码语言:javascript
复制
旋转屏幕时触发的函数:
//旋转方向发生改变时
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
//视图旋转动画前一半发生之前自动调用
-(void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
//视图旋转动画后一半发生之前自动调用
-(void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {
}
//视图旋转之前自动调用
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
//视图旋转完成之后自动调用
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
}
//视图旋转动画前一半发生之后自动调用
-(void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
}

如果项目中用了navigationViewController, 那么就应该新建一个uinavigationViewController的子类,然后在这个类里面写上下面的代码,在使用的时候就用自定义的这个navCtr, 就是说需要在根视图里面控制

代码语言:javascript
复制
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
return toInterfaceOrientation != UIDeviceOrientationPortraitUpsideDown; 
} 
 - (BOOL)shouldAutorotate { 
if ([self.topViewController isKindOfClass:[AddMovieViewController class]]) {       // 如果是这个 vc 则支持自动旋转 
return YES; 
} 
return NO; 
} 
- (NSUInteger)supportedInterfaceOrientations { 
return UIInterfaceOrientationMaskAllButUpsideDown; 
}

1.Window级别的控制 对于UIApplicationDelegate的这个方法声明,大多数情况下application就是当前的application,而window通常也只有一个。所以基本上通过window对横屏竖屏interfaceOrientation的控制相当于全局的。 //每次试图切换的时候都会走的方法,用于控制设备的旋转方向. - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (_isRotation) { return UIInterfaceOrientationMaskLandscape; }else { return UIInterfaceOrientationMaskPortrait; } }


2.Controller层面的控制 //哪些页面支持自动转屏 - (BOOL)shouldAutorotate{

代码语言:javascript
复制
return YES;
}
// viewcontroller支持哪些转屏方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskAllButUpsideDown;
 }

3.使得特定ViewController坚持特定的interfaceOrientation. 当然,使用这个方法是有前提的,就是当前ViewController是通过全屏的 Presentation方式展现出来的. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);


4.当前屏幕方向interfaceOrientation的获取.

代码语言:javascript
复制
 有3种方式可以获取到“当前interfaceOrientation”:
controller.interfaceOrientation,获取特定controller的方向
[[UIApplication sharedApplication] statusBarOrientation] 获取状态条相关的方向
[[UIDevice currentDevice] orientation] 获取当前设备的方向
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.05.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档