zoukankan      html  css  js  c++  java
  • ios 检测屏幕方向

    方法一:通知中心监听

    name:

    // UIDeviceOrientationDidChangeNotification     允许方向改变的情况下,监听设备方向,与电池条无关

    // UIApplicationDidChangeStatusBarOrientationNotification      允许方向改变的情况下,监听电池条方向

    -(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); }

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

    另外:viewcontrller内部控制及代理

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return YES;
    }
    
    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskAll;
    }
    //
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        NSLog(@"UIViewController will rotate to Orientation: %d", toInterfaceOrientation);
    }
    
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        NSLog(@"did rotated to new Orientation, view Information %@", self.view);
    }

    appdelegate中有相应改变其旋转方向的方法

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
       if (self.allowRotation) {
          return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
       }
       return UIInterfaceOrientationMaskPortrait;
    }
    

    方法二:陀螺仪监测

    - (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]; }
  • 相关阅读:
    单例模式
    简单的WPS二次开发脚本
    使用DevExpress改变WinForm皮肤(VS)
    步入DevExpress的使用(VS)
    设置PdfPTable与标题间的距离
    tar: Removing leading `/’ from member names
    MySQL关闭过程详解和安全关闭MySQL的方法
    Nikto是一款Web安全扫描工具,可以扫描指定主机的web类型,主机名,特定目录,cookie,特定CGI漏洞,XSS漏洞,SQL注入漏洞等,非常强大滴说。。。
    解决Centos关闭You have new mail in /var/spool/mail/root提示
    hping3命令
  • 原文地址:https://www.cnblogs.com/chaochaobuhuifei55/p/5438337.html
Copyright © 2011-2022 走看看