根据重力来判断手机屏幕旋转(在手机锁定屏幕旋转下

导入 :

#import

增加属性:

@property (strong, nonatomic) CMMotionManager *motionManager;
@property (assign, nonatomic) BOOL isLandScape;

初始化:

self.isLandScape = NO;
[self initMotionManager];

在真机关闭屏幕旋转功能时如何去判断屏幕方向:

-- (void)initMotionManager
{
    if (_motionManager == nil) 
    {
          _motionManager = [[CMMotionManager alloc] init];
    }
    // 提供设备运动数据到指定的时间间隔
    _motionManager.deviceMotionUpdateInterval = .3;
    //    _motionManager.accelerometerAvailable
    if (_motionManager.deviceMotionAvailable) 
    {  
            // 确定是否使用任何可用的态度参考帧来决定设备的运动是否可用
            // 启动设备的运动更新,通过给定的队列向给定的处理程序提供数据。
            [_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) 
            {
                 if (self.wmPlayer && self.wmPlayer.superview)
                 {
                       [self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
                  }
            }];
      }
      else
      {
            [self setMotionManager:nil];
      }
}

- (void)handleDeviceMotion:(CMDeviceMotion *)deviceMotion
{
      double x = deviceMotion.gravity.x;
      double y = deviceMotion.gravity.y;
      if (fabs(y) >= fabs(x))
      {  
            //NSLog(@"竖屏");
            if (self.isLandScape == YES) 
            {
                  [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
                  //这句话是防止手动先把设备置为竖屏,导致下面的语句失效.
                  [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
                  self.isLandScape = NO;
                  [self toCell];
             }
       }
       else
       {        
             //NSLog(@"横屏");
             if (self.isLandScape == NO) 
             {
                  [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
                  // 这句话是防止手动先把设备置为横屏,导致下面的语句失效.
                  if(x > 0)
                  {
                        // 左边在上
                        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeRight] forKey:@"orientation"];
                        [self toFullScreenWithInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];
                  }
                  else
                  {
                        // 右边在上
                        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
                        [self toFullScreenWithInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
                  }
                  self.isLandScape = YES;
              }
        }
}


强烈推荐:超简单!!! iOS设置状态栏、导航栏按钮、标题、颜色、透明度,偏移等

https://github.com/wangrui460/WRNavigationBar
https://github.com/wangrui460/WRNavigationBar_swift



欢迎关注我的微博:wangrui460

你可能感兴趣的:(根据重力来判断手机屏幕旋转(在手机锁定屏幕旋转下)