根據(jù)重力來判斷手機屏幕旋轉(zhuǎn)(在手機鎖定屏幕旋轉(zhuǎn)下

導(dǎo)入 :

#import <CoreMotion/CoreMotion.h>

增加屬性:

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

初始化:

self.isLandScape = NO;
[self initMotionManager];

在真機關(guān)閉屏幕旋轉(zhuǎn)功能時如何去判斷屏幕方向:

-- (void)initMotionManager
{
    if (_motionManager == nil) 
    {
          _motionManager = [[CMMotionManager alloc] init];
    }
    // 提供設(shè)備運動數(shù)據(jù)到指定的時間間隔
    _motionManager.deviceMotionUpdateInterval = .3;
    //    _motionManager.accelerometerAvailable
    if (_motionManager.deviceMotionAvailable) 
    {  
            // 確定是否使用任何可用的態(tài)度參考幀來決定設(shè)備的運動是否可用
            // 啟動設(shè)備的運動更新,通過給定的隊列向給定的處理程序提供數(shù)據(jù)。
            [_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"];
                  //這句話是防止手動先把設(shè)備置為豎屏,導(dǎo)致下面的語句失效.
                  [[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"];
                  // 這句話是防止手動先把設(shè)備置為橫屏,導(dǎo)致下面的語句失效.
                  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設(shè)置狀態(tài)欄、導(dǎo)航欄按鈕、標題、顏色、透明度,偏移等

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



歡迎關(guān)注我的微博:wangrui460

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,190評論 4 61
  • [toc] JSON必知必會 學(xué)習(xí)網(wǎng)站:SafariBooksOnline 它是什么?我可以用它做什么?那些別有用...
    z嘉嘉嘉閱讀 1,125評論 0 11
  • 今天情緒異常的低落,連上日的陰雨天使之越發(fā)深刻。 二十多年來,活得越來越孤獨,姐姐說我性格變得越來越孤僻了,我承認...
    anonymous100000閱讀 316評論 0 0
  • 偏若驚鴻影, 一顰一笑搖曳了星云, 柳下聞瑤琴起舞和一曲, 心語留香侯人尋, 你送我的風(fēng)景, 江山如畫又怎能比擬。
    清竹霄霄z十只鷹貿(mào)易閱讀 303評論 0 0
  • 故事梗概:李凱文告訴了高偉民陶家佳麗不可能注意于他,也跟她說“天涯何處無芳草”,高偉民不信,同桌陸鑫戲謔地向高偉民...
    豫蘭劍客閱讀 574評論 0 0

友情鏈接更多精彩內(nèi)容