【问题标题】:how can i know the device orientation programmatically when user locked screen rotation当用户锁定屏幕旋转时,我如何以编程方式知道设备方向
【发布时间】:2016-05-23 07:33:49
【问题描述】:

在我的相机项目中,我想知道系统相机等用户锁定屏幕旋转的情况下的设备方向。

我如何才能知道设备方向或仅为我的项目解锁屏幕旋转??

【问题讨论】:

    标签: ios camera avfoundation orientation


    【解决方案1】:

    您可以为此使用 CoreMotion。 签出这个库here

    【讨论】:

      【解决方案2】:

      您可能会找到几种方法来获取当前设备方向或方向变化here

      【讨论】:

      • 这个不行,当屏幕旋转锁定时,UIDevice.currentDevice().orientation一直是之前的状态,这次我们无法捕捉到旋转动作。
      • CoreMotion 可以提供帮助
      【解决方案3】:

      感谢 Xcoder,coreMotion 帮助了我。

      并遵循我的代码:

      - (void)startMotionManager{
          if (_motionManager == nil) {
              _motionManager = [[CMMotionManager alloc] init];
          }
          _motionManager.deviceMotionUpdateInterval = 1/2.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) {
                  self.deviceOrientation = UIDeviceOrientationPortraitUpsideDown;
              } else {
                  self.deviceOrientation = AVCaptureVideoOrientationPortrait;
              }
          } else {
              if (x >= 0) {
                  self.deviceOrientation = UIDeviceOrientationLandscapeRight;
              }
              else {
                  self.deviceOrientation = UIDeviceOrientationLandscapeLeft;
              }
          }
      } 
      

      【讨论】:

        猜你喜欢
        • 2012-11-10
        • 1970-01-01
        • 1970-01-01
        • 2016-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-28
        • 1970-01-01
        相关资源
        最近更新 更多