ios开发过程中屏幕方向判断的问题

本文介绍了几种实用的iOS屏幕方向判断方法,包括利用设备API、屏幕尺寸计算等,并提供了具体的实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

判断屏幕的方法有很多着及仅提供几个我个人认为好用的方案

[objc]  view plain  copy
  1. Landscape 竖屏  
  2. Portrait  横屏  
  3.   
  4.   
  5.   
  6. 最有效的方法是:  
  7. 在willRotateToInterfaceOrientation:duration:  
  8. 方法中将方向存储起来:  
  9. DrviceOrientation = toInterfaceOrientation;  
  10. 然后在别的方法中使用相应的屏幕的方向  
  11.   
  12. 方法一:  
  13. 直接获取设备的方法:self.interfaceOrientation(此方法已经过期)  
  14. 方法二:  
  15. 通过下面的方法:  
  16. UIDeviceOrientation duration = [[UIDevice currentDevice]orientation];  
  17.   
  18. 方法12当在模拟器中运行时,刚开始获得的设备方向为UnKnow  
  19.   
  20. 方法三  
  21. 跟据屏幕的宽度计算相应的屏幕的方向  
  22. [[UIScreenmainScreen] applicationFrame].size.height  
  23. [[UIScreenmainScreen] applicationFrame].size.width  
  24. 可以用来获取当前屏幕的尺寸,高和宽。由于系统的状态条占高20且总是在屏幕上方,它使得上面两个值在横竖屏的时候有变化,因此可用来判断当前是横屏还是竖屏。  
  25. 简单的说竖屏时,height为1004,width为768。  
  26. 横屏时,height为1024,width为748。  
  27. 当然 ,前提是你没有把系统的状态栏去掉.它可以用在任何方法内作为判断条件.  
  28. 应用示例如下:  
  29. if (loadingview ==nil)  
  30. {  
  31.     loadingview = [[UIViewalloc] initWithFrame:CGRectMake(284402200200)];  
  32.     if ([[UIScreenmainScreen] applicationFrame].size.height==1024)  
  33.     {  
  34.         loadingview.frame=CGRectMake(412264200200);//此时为横屏  
  35.     }  
  36.     [loadingviewsetBackgroundColor:[UIColorclearColor]];  
  37. }  
  38.   
  39. //创建loadingview的时候根据当前横竖屏设定位置。  
  40.   
  41. 方法四  在论坛里已经有人发过了(我在这里仅做了简单的搬运工作)  
  42. //下面则是直接以屏幕方向判断  
  43. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {  
  44.   
  45.   
  46. switch (interfaceOrientation) {  
  47.     caseUIInterfaceOrientationPortrait:  
  48.         //home健在下  
  49.         break;  
  50.     caseUIInterfaceOrientationPortraitUpsideDown:  
  51.         //home健在上  
  52.         break;  
  53.     caseUIInterfaceOrientationLandscapeLeft:  
  54.         //home健在左  
  55.         break;  
  56.     caseUIInterfaceOrientationLandscapeRight:  
  57.         //home健在右  
  58.         break;  
  59.     default:  
  60.         break;  
  61.          
  62. }  
  63. 方法五  
  64. 再屏幕旋转的时候记录下来屏幕的方向 着样做的画需要再窗后启动的时候让屏幕进行一次旋转,否则的画当程序启动的时候 屏幕的方向会有问题  
  65. #pragma mark 屏幕旋转完成  
  66. -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{  
  67.     NSLog(@"屏幕旋转完成 dockView的高度%f",self.dockView.height);  
  68.     //self.dockView.width = self.dockWidth;  
  69. }  
  70. #pragma mark 屏幕将旋转  
  71. -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{  
  72.     [UIView animateWithDuration:0.25 animations:^{  
  73.          
  74.         self.dockView.width = self.dockWidth;  
  75.     }];  
  76.      
  77.     // 判断横竖屏  
  78.     if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {  
  79.         NSLog(@"屏幕将旋转 为 竖屏");  
  80.         self.dockView.width = self.dockWidth;  
  81.     }else{  
  82.         NSLog(@"屏幕将旋转 为 横屏");  
  83.         self.dockWidth = 100;  
  84.     }  
  85. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值