ios 检测屏幕方向

方法一:通知中心监听

-(void)notifitionSatatus{    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
-(void)statusBarOrientationChange:(NSNotification*)notification{
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationLandscapeRight) // home键靠右
    {

    }
    if (orientation ==UIInterfaceOrientationLandscapeLeft) // home键靠左
    {

    }
    if (orientation == UIInterfaceOrientationPortrait)//home在下
    {

    }
    if (orientation == UIInterfaceOrientationPortraitUpsideDown)
    {

    }
    NSLog(@"%ld",orientation);
}

注意:在没有打开屏幕旋转开关的情况下,该通知无效

方法二:陀螺仪监测

- (void)startMotionManager{
    if (_motionManager == nil) {
        _motionManager = [[CMMotionManager alloc] init];
    }  //检测时间间隔
    _motionManager.deviceMotionUpdateInterval = 1.0;
    if (_motionManager.deviceMotionAvailable) {
        NSLog(@"Device Motion Available");
        [_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                            withHandler: ^(CMDeviceMotion *motion, NSError *error){
                                                [self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];

                                            }];
    } else {
        NSLog(@"No device motion on device.");
        [self setMotionManager:nil];
    }
}
- (void)handleDeviceMotion:(CMDeviceMotion *)deviceMotion{
    double x = deviceMotion.gravity.x;
    double y = deviceMotion.gravity.y;
    if (fabs(y) >= fabs(x))
    {
        if (y >=0){
             //UIDeviceOrientationPortraitUpsideDown;
            [self.delegate actionWithDeviceOrientation:UIDeviceOrientationPortraitUpsideDown];
        }
        else{
            // UIDeviceOrientationPortrait;
            [self.delegate actionWithDeviceOrientation:UIDeviceOrientationPortrait];
        }
    }
    else
    {
        if (x >= 0){
            // UIDeviceOrientationLandscapeRight;
            [self.delegate actionWithDeviceOrientation:UIDeviceOrientationLandscapeRight];
        }
        else{
            // UIDeviceOrientationLandscapeLeft;
            [self.delegate actionWithDeviceOrientation:UIDeviceOrientationLandscapeLeft];
        }
    }
}//停止检测
-(void)endMotionManager{
    [_motionManager stopDeviceMotionUpdates];
}
时间: 2024-07-31 11:37:01

ios 检测屏幕方向的相关文章

IOS UIDevice & IOS检测屏幕旋转实例

一 UIDevice 简介 UIDevice类提供了一个单例实例代表当前的设备.从这个实例中可以获得的信息设备,比如操作系统名称.电池电量值(batteryLevel).电池状态(batteryState).设备的类型(model,比如iPod.iPhone等).设备的系统(systemVersion) 二 获取 UIDevice 实例 通过[UIDevice currentDevice]可以获取这个单粒对象 UIDevice *device = [UIDevice currentDevice]

ios 设置屏幕方向的两种方法

第一种:通过人为的办法改变view.transform的属性. 具体办法: view.transform一般是View的旋转,拉伸移动等属性,类似view.layer.transform,区别在于View.transform是二维的,也就是使用仿射的办法通常就是带有前缀CGAffineTransform的类(可以到API文档里面搜索这个前缀的所有类),而view.layer.transform可以在3D模式下面的变化,通常使用的都是前缀为CATransform3D的类. 这里要记住一点,当你改变

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

判断屏幕的方法有很多着及仅提供几个我个人认为好用的方案 Landscape 竖屏 Portrait 横屏 最有效的方法是: 在willRotateToInterfaceOrientation:duration: 方法中将方向存储起来: DrviceOrientation = toInterfaceOrientation; 然后在别的方法中使用相应的屏幕的方向 方法一: 直接获取设备的方法:self.interfaceOrientation(此方法已经过期) 方法二: 通过下面的方法: UIDev

js监听屏幕方向如何第一次默认不监听

this.supportOrientation = typeof window.orientation === 'number'; // 检查屏幕方向 checkScreenOrientation() { if (this.supportOrientation) { if (window.orientation === 0) { this.screenOrientation = 'portrait'; } if (window.orientation === 90 || window.orien

UI第三讲.自定义视图 视图控制器指定自定义view 检测屏幕旋转 处理内存警告 容器视图控制器

一.自定义视图 (自定义label-textField视图) 目的:为了进一步优化登录界面,提高代码的精简程度和复用性,可移植性,从而需要在原有视图控件的基础之上自由组合成自定义视图. 一般自定义的视图会继承于UIView.以下是自定义视图的要点和步骤: 1.创建一个UIView子类 2.在类的初始化方法中添加子视图 3.类的.h文件提供一些接口(方法),便于外界操作子视图. 例子及相应代码: 例题:假设我们使用LTView类代表label-textfield视图.创建一个LTView类继承于U

屏幕方向该知道的那些事儿

前言 这两天在学关于屏幕旋转的相关的知识,也延伸出了加速器和陀螺仪这些以前没有深入去学习过的知识点,在没有仔细看之前也有一些问题在想,比如:用户关闭了手机的屏幕旋转,但根据我们的使用经验,APP的界面还是可以旋转的,比如那些视屏播放类型的APP,还是可以全屏观看视频的,那这些是怎么做的?还有比如 你整个项目不允许横屏展示的,而某一个控制器却单独要求横屏展示,这个又该怎么做?用户关闭了手机屏幕旋转,我们还能不能判断手机屏幕的方向?带着这些问题我们一个一个的说一下屏幕方向的那些事儿. 从简单的开始

UI: 概述, 启动屏幕, 屏幕方向

UI 设计概述 启动屏幕(闪屏) 屏幕方向 示例1.UI 设计概述UI/Summary.xaml <Page x:Class="Windows10.UI.Summary" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local=&qu

iOS实现屏幕旋转

iOS实现屏幕旋转有两种方式 1. 应用本身支持 2. 手动旋转UIView (这种方式我没找到旋转 系统控件的方法 如:键盘.导航.UIAlertView) 如果你只是要把竖屏的播放器,做成支持横屏的,没有其他界面操作, 就可以考虑用第二种方式去做,比较简单 ,不过要注意计算view Frame 这两种方式看你具体的使用场景了,具体场景选择合适的方式. 公司项目中有几个界面要支持横竖屏,(直播录制界面.直播观看界面.视频回看界面). 刚开始我想着用第二种方式去解决,但是我们视频录制.观看界面有

Windows Phone开发(6):处理屏幕方向的改变

俺们都知道,智能手机可以通过旋转手机来改变屏幕的显示方向,更多的时候,对于屏幕方向的改变,我们要做出相应的处理,例如,当手机屏幕方向从纵向变为横向时,可能要重新排列页面上的控件以适应显示区域的变化. 前面我们讨论过,Silverlight for Windows Phone的页面布局有三个常用的布局控件,那么,当屏幕方向改变后,我们所做的对布局的更改基础上是基于这几个容器进行的操作. 本文我将通过三个示例来分别说明. 开始之前,先说一下PhoneApplicationPage类的Orientat