ios - 有什么方法可以检测平面位置的手机角度吗?
问题描述
即使手机处于纵向锁定状态并尝试通过将其保持在右手(横向)位置或左手位置(横向)来捕获图像,是否有任何方法可以获取手机的当前角度?
每当我通过右手位置捕获图像(将主页按钮保持在右侧)时,我都会正确获取它,但是每当我通过左手位置捕获图像时,我都会得到倒置图像(图像旋转 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];
再捕获不应该旋转图像。
解决方案
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape {
print("Landscape")
} else {
print("Portrait")
}
}
希望这对你有帮助
推荐阅读
- fable-f# - Fable Elmish 中的属性与属性
- javascript - mpld3 在同一个 html 页面上使用 d3v3 和 d3v4
- firebase - 为什么 Smart TV Web 应用上会发生此 Firebase IDBIndex 错误,是否有解决方法?
- mysql - 更改在生产中运行的 4M+ 条目表上的列
- javascript - 合并来自不同组件的 observables
- java - 在程序代码中而不是从文件中手动加载我的数组是个好主意吗?
- javascript - 根据 Google Apps 脚本中的另一个数组过滤数组
- python - Django 2.2 错误:int() 参数必须是字符串、类似字节的对象或数字,而不是“列表”
- javascript - 如何将 Jquery 代码转换为纯 JavaScript 代码
- android - OneSignal Web 查看基于用户的通知