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

导入 :

#import <CoreMotion/CoreMotion.h>

增加属性:

@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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,241评论 4 61
  • [toc] JSON必知必会 学习网站:SafariBooksOnline 它是什么?我可以用它做什么?那些别有用...
    z嘉嘉嘉阅读 1,084评论 0 11
  • 今天情绪异常的低落,连上日的阴雨天使之越发深刻。 二十多年来,活得越来越孤独,姐姐说我性格变得越来越孤僻了,我承认...
    anonymous100000阅读 296评论 0 0
  • 偏若惊鸿影, 一颦一笑摇曳了星云, 柳下闻瑶琴起舞和一曲, 心语留香侯人寻, 你送我的风景, 江山如画又怎能比拟。
    清竹霄霄z十只鹰贸易阅读 261评论 0 0
  • 故事梗概:李凯文告诉了高伟民陶家佳丽不可能注意于他,也跟她说“天涯何处无芳草”,高伟民不信,同桌陆鑫戏谑地向高伟民...
    豫兰剑客阅读 400评论 0 0