打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
IOS疯狂基础之屏幕旋转控制,获得当前方向

获得当前屏幕方向

self.interfaceOrientation或[[UIApplication sharedApplication] statusBarOrientation]

 if (self.interfaceOrientation==UIDeviceOrientationLandscapeRight) {

XXOO

}

不旋转,保持竖屏

//iOS 5- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{        return (toInterfaceOrientation == UIInterfaceOrientationPortrait);}//iOS 6- (BOOL)shouldAutorotate{        return NO;}- (NSUInteger)supportedInterfaceOrientations{        return UIInterfaceOrientationMaskPortrait;}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{        return UIInterfaceOrientationPortrait;}

始终保持横屏

//iOS 5- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{        return (toInterfaceOrientation == self.preferredInterfaceOrientationForPresentation);}//iOS 6- (BOOL) shouldAutorotate{        return YES;}- (NSUInteger)supportedInterfaceOrientations{        return UIInterfaceOrientationMaskLandscapeRight;}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{        return UIInterfaceOrientationLandscapeRight;}

在使用UINavigationController时发现,无论怎么设置上面方法的返回,都无法控制UINavigationController的旋转,这似乎是iOS的一个bug。但无论如何,以下是stackflow上面的一个解决方法

 1 @implementation UINavigationController (Rotation_IOS6) 2 -(BOOL)shouldAutorotate { 3     return [[self.viewControllers lastObject] shouldAutorotate]; 4 } 5  6 -(NSUInteger)supportedInterfaceOrientations { 7     return [[self.viewControllers lastObject] supportedInterfaceOrientations]; 8 } 9 10 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {11     return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];12 }13 @end



屏幕旋转方法调用流程

要翻转的时候,首先响应的方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

return YES则支持翻转,NO则不支持。

紧接着

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

被调用。这个方法是发生在翻转开始之前。一般用来禁用某些控件或者停止某些正在进行的活动,比如停止视频播放。
再来是

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

这个方法发生在翻转的过程中,一般用来定制翻转后各个控件的位置、大小等。可以用另外两个方法来代替:willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:   和  willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:

最后调用的是

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

这个方法发生在整个翻转完成之后。一般用来重新启用某些控件或者继续翻转之前被暂停的活动,比如继续视频播放



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ios6 屏幕旋转总结
iOS系统自带的 **UIAlertView** 以及 屏幕自动旋转的实现
iOS横竖屏解决方案
Ipad开发课程-Charpter2 第一讲:我们的第一个iPad程序,“Hello World!” ? 疯狂的老九
iOS学习笔记(3)
IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服