有什么方法可以检测到手机在平坦位置时的倾角?

时间:2019-05-13 05:19:35

标签: ios objective-c orientation portrait

即使手机处于纵向锁定状态,也可以通过将其保持在右手(横向)位置或左手位置(横向)来获取图像的当前角度吗?

每当我通过右手位置捕获图像(保持右侧的主页按钮)时,都能正确获取图像,但是每当我通过左手位置捕获图像时,其图像就会反转(图像旋转180度) 。如何解决此问题?

我已经尝试过了,但是没有用,因为becoz的方向永远是纵向

UIDeviceOrientation orientation=[[UIDevice currentDevice] orientation];

     // Do Orientation operations
     if (orientation == UIDeviceOrientationLandscapeRight)
     {
         MYLog(@"LandRight");
         rotation = [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(90 * (M_PI/180))];
     }
     else if(orientation == UIDeviceOrientationLandscapeLeft){
         MYLog(@"LandLeft");
         rotation = [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(-90 * (M_PI/180))];
     }
     else if(orientation == UIDeviceOrientationPortrait){
         MYLog(@"porttttt");
         rotation = [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(0 * (M_PI/180))];
     }
     else if(orientation == UIDeviceOrientationPortraitUpsideDown)
     {
         MYLog(@"portupdown");
         rotation = [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(1 * (M_PI/180))];
     }

     else if(orientation == UIDeviceOrientationFaceUp)
     {
         MYLog(@"faceup");
         rotation = [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(-90 * (M_PI/180))];
     }
     [transform setValue:rotation forKey:@"inputTransform"];
     enhancedImage = [transform outputImage];

捕捉到的图像完全不会旋转。

2 个答案:

答案 0 :(得分:1)

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.orientation.isLandscape {
        print("Landscape")
    } else {
        print("Portrait")
    }
}

希望这对您有帮助

答案 1 :(得分:0)

您可以结合使用CoreMotion库和一些逻辑来决定原始陀螺仪事件的方向。 https://developer.apple.com/documentation/coremotion 这是来自Apple的一些示例应用程序:https://developer.apple.com/library/archive/samplecode/MotionGraphs/Introduction/Intro.html