首页 > 解决方案 > 有什么方法可以检测平面位置的手机角度吗?

问题描述

即使手机处于纵向锁定状态并尝试通过将其保持在右手(横向)位置或左手位置(横向)来捕获图像,是否有任何方法可以获取手机的当前角度?

每当我通过右手位置捕获图像(将主页按钮保持在右侧)时,我都会正确获取它,但是每当我通过左手位置捕获图像时,我都会得到倒置图像(图像旋转 180 度)。如何解决这个问题?

我试过这个但没用,因为方向总是纵向的

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];

再捕获不应该旋转图像。

标签: iosobjective-corientationportrait

解决方案


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

希望这对你有帮助


推荐阅读