判断屏幕的方法有很多着及仅提供几个我个人认为好用的方案
- Landscape 竖屏
- Portrait 横屏
- 最有效的方法是:
- 在willRotateToInterfaceOrientation:duration:
- 方法中将方向存储起来:
- DrviceOrientation = toInterfaceOrientation;
- 然后在别的方法中使用相应的屏幕的方向
- 方法一:
- 直接获取设备的方法:self.interfaceOrientation(此方法已经过期)
- 方法二:
- 通过下面的方法:
- UIDeviceOrientation duration = [[UIDevice currentDevice]orientation];
- 方法1、2当在模拟器中运行时,刚开始获得的设备方向为UnKnow
- 方法三
- 跟据屏幕的宽度计算相应的屏幕的方向
- [[UIScreenmainScreen] applicationFrame].size.height
- [[UIScreenmainScreen] applicationFrame].size.width
- 可以用来获取当前屏幕的尺寸,高和宽。由于系统的状态条占高20且总是在屏幕上方,它使得上面两个值在横竖屏的时候有变化,因此可用来判断当前是横屏还是竖屏。
- 简单的说竖屏时,height为1004,width为768。
- 横屏时,height为1024,width为748。
- 当然 ,前提是你没有把系统的状态栏去掉.它可以用在任何方法内作为判断条件.
- 应用示例如下:
- if (loadingview ==nil)
- {
- loadingview = [[UIViewalloc] initWithFrame:CGRectMake(284, 402, 200, 200)];
- if ([[UIScreenmainScreen] applicationFrame].size.height==1024)
- {
- loadingview.frame=CGRectMake(412, 264, 200, 200);//此时为横屏
- }
- [loadingviewsetBackgroundColor:[UIColorclearColor]];
- }
- //创建loadingview的时候根据当前横竖屏设定位置。
- 方法四 在论坛里已经有人发过了(我在这里仅做了简单的搬运工作)
- //下面则是直接以屏幕方向判断
- - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
- switch (interfaceOrientation) {
- caseUIInterfaceOrientationPortrait:
- //home健在下
- break;
- caseUIInterfaceOrientationPortraitUpsideDown:
- //home健在上
- break;
- caseUIInterfaceOrientationLandscapeLeft:
- //home健在左
- break;
- caseUIInterfaceOrientationLandscapeRight:
- //home健在右
- break;
- default:
- break;
- }
- 方法五
- 再屏幕旋转的时候记录下来屏幕的方向 着样做的画需要再窗后启动的时候让屏幕进行一次旋转,否则的画当程序启动的时候 屏幕的方向会有问题
- #pragma mark 屏幕旋转完成
- -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
- NSLog(@"屏幕旋转完成 dockView的高度%f",self.dockView.height);
- //self.dockView.width = self.dockWidth;
- }
- #pragma mark 屏幕将旋转
- -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
- [UIView animateWithDuration:0.25 animations:^{
- self.dockView.width = self.dockWidth;
- }];
- // 判断横竖屏
- if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
- NSLog(@"屏幕将旋转 为 竖屏");
- self.dockView.width = self.dockWidth;
- }else{
- NSLog(@"屏幕将旋转 为 横屏");
- self.dockWidth = 100;
- }
- }